Reputation: 61
#include <stdio.h>
void lergab(char *gab);
void criamatriz (char **alunoresp, int *resp);
int main(){
char gab[20];
char alunoresp [100][21];
int resp[80];
//lergab(&gab);
criamatriz (&alunoresp[100][21], &resp[80]);
printf ("\n\n%d\n", alunoresp[0][0]);
printf ("%c\n", alunoresp[0][1]);
printf ("%c\n", alunoresp[0][1]);
printf ("%c\n", alunoresp[0][19]);
return 0;
}
void lergab (char *gab){
int i;
for(i=0; i<20; i++)
scanf("%c\n" , &gab[i]);
printf("%c", gab[19]);
}
void criamatriz (char **alunoresp,int *resp){
int i = 0, j;
//for (i = 0; i <= 100; i++){
scanf ("%c", &alunoresp[i][0]);
for (j = 0; j < 80; j++){
scanf ("%d", &resp[j]);
}
for (j = 0; j < 80; j=j+4){
int g = 1;
if ((resp[j] + resp[j+1] + resp[j+2] + resp[j+3]) != 1)
alunoresp[i][g] = 'e';
else if (resp[j] == 1)
alunoresp[i][g] = 'a';
else if (resp[j+1] == 1)
alunoresp[i][g] = 'b';
else if (resp[j+2] == 1)
alunoresp[i][g] = 'c';
else if (resp[j+3] == 1)
alunoresp[i][g] = 'd';
g++;
}
//}
}
This code is supposed to read a number as ASCII code, then read 80 numbers and convert them to their respective character, like a electronic test scanner. I.E: 0 0 1 0 is c, 1 0 0 0 is a, 1 0 1 0 is an invalid option (for you should only sign one answer per question).
Code is just a test, my objective here is create a matrix that contains 100 candidates with 20 answer in each of them, along with their identification code, which is the first number read as ASCII, thus alunoresp[100][21] is declared. I'm getting segmentation error every time i try to run it on gcc, any ideas?
Edit:
Thanks for the help on the indices thing. I didn't notice. Stil, even after fixing that i keep getting segmentation faults.
Code now looks like this:
#include <stdio.h>
void lergab(char *gab);
void criamatriz (char **alunoresp, int *resp);
int main(){
char gab[20];
char alunoresp [100][21];
int resp[80];
//lergab(&gab);
criamatriz (&alunoresp, &resp);
printf ("\n\n%d\n", alunoresp[0][0]);
printf ("%c\n", alunoresp[0][1]);
printf ("%c\n", alunoresp[0][1]);
printf ("%c\n", alunoresp[0][19]);
return 0;
}
void lergab (char *gab){
int i;
for(i=0; i<20; i++)
scanf("%c\n" , &gab[i]);
printf("%c", gab[19]);
}
void criamatriz (char **alunoresp,int *resp){
int i = 0, j;
//for (i = 0; i <= 100; i++){
scanf ("%c", &alunoresp[i][0]);
for (j = 0; j < 80; j++){
scanf ("%d", &resp[j]);
}
for (j = 0; j < 80; j=j+4){
int g = 1;
if ((resp[j] + resp[j+1] + resp[j+2] + resp[j+3]) != 1)
alunoresp[i][g] = 'e';
else if (resp[j] == 1)
alunoresp[i][g] = 'a';
else if (resp[j+1] == 1)
alunoresp[i][g] = 'b';
else if (resp[j+2] == 1)
alunoresp[i][g] = 'c';
else if (resp[j+3] == 1)
alunoresp[i][g] = 'd';
g++;
}
//}
}
Upvotes: 1
Views: 98
Reputation: 14619
Welcome to SO, Lucas!
I'm getting segmentation error every time i try to run it on gcc, any ideas?
Step 0 is to get yourself a debugger, this won't be the last time you encounter a similar issue. If you're using gcc
, it's a good bet that gdb
is available on this system. Try running your executable with gdb
and X marks the spot!
gdb ./my_binary
... then ..
run
execution will halt right where the invalid memory access occurred. You can examine the state of the program (local variables, etc) using the print
command and you can walk up and down the stack with up
and down
respectively.
For example:
(gdb) run
Starting program: ./go
Program received signal SIGSEGV, Segmentation fault.
0x00000000004004fd in inner () at ./go.c:6
6 int ohno = *bad;
(gdb) bt
#0 0x00000000004004fd in inner () at ./go.c:6
#1 0x0000000000400512 in outer () at ./go.c:11
#2 0x000000000040052d in main (argc=1, argv=0x7fffffffdf88) at ./go.c:16
(gdb) backtrace
#0 0x00000000004004fd in inner () at ./go.c:6
#1 0x0000000000400512 in outer () at ./go.c:11
#2 0x000000000040052d in main (argc=1, argv=0x7fffffffdf88) at ./go.c:16
Upvotes: 1
Reputation: 726569
There are two issues with your program:
criamatriz
function incorrectly - rather than passing a pointer to the end of the arrays, you should be passing arrays themselves, andcriamatriz
function takes a 2D array incorrectly - you are passing it like a pointer to a pointer, which is incorrect. You should be passing it like a pointer to an array of 21 elements.Here is how you should declare and define your criamatriz
function:
void criamatriz(char (*alunoresp)[21], int *resp);
Call it like this:
criamatriz(alunoresp, resp);
Upvotes: 3
Reputation: 360662
criamatriz (&alunoresp[100][21], &resp[80]);
^^^^^^^^^
You're exceeding the maximum allowed indeces in your array there. e.g.
int x[10]; // allows index 0->9
Upvotes: 2
Reputation: 12270
When sending an array to a function, all you need to do is send the base address of that array,
In your code
criamatriz (&alunoresp[100][21], &resp[80]);
this line is actually sending the address of the alunoresp[100][21]
element, which doesnt even exist.
An array defined as int alunoresp[100][21]
has maximum indices [99][20]
since indices start from 0
You need to call the function like this,
criamatriz (alunoresp, resp);
Since the name of the array contains the base address (address poitning to the first element) of that array.
Upvotes: 0