That code isn't compilable. Compilable means it compiles, and hopefully it does so without any warnings, even at the maximum warning level
Code:
$ make foo
gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
foo.c: In function ‘getInput’:
foo.c:11:12: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
foo.c:11:27: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
foo.c:12:10: error: ‘i’ undeclared (first use in this function)
foo.c:12:10: note: each undeclared identifier is reported only once for each function it appears in
foo.c:14:9: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
foo.c:14:9: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
foo.c:15:9: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
foo.c:15:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
foo.c:15:42: error: ‘Info’ undeclared (first use in this function)
foo.c: In function ‘main’:
foo.c:22:5: error: expected ‘,’ or ‘;’ before ‘struct’
foo.c:23:5: error: ‘Info’ undeclared (first use in this function)
foo.c:24:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
make: *** [foo] Error 1
Thus, I can't be sure that the changes I make to get it working will result in the exact same code you're using, that causes problems. I may find a problem my way, but it might not be the same problem.
The easy fixes were adding the #includes for stdio.h and stdlib.h, adding the missing ; on the declaration of n in main and declaring i in getInput. However, I can't quite be certain what you're doing with the printf in getInput. I think Info should be test, but that's a guess.
Doing all that still leaves these problems:
Code:
$ make foo
gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
foo.c: In function ‘getInput’:
foo.c:18:36: error: dereferencing pointer to incomplete type
foo.c:18:48: error: dereferencing pointer to incomplete type
foo.c:18:60: error: dereferencing pointer to incomplete type
foo.c:18:73: error: dereferencing pointer to incomplete type
foo.c:19:50: error: dereferencing pointer to incomplete type
foo.c:19:62: error: dereferencing pointer to incomplete type
foo.c:19:74: error: dereferencing pointer to incomplete type
foo.c:19:86: error: dereferencing pointer to incomplete type
foo.c: In function ‘main’:
foo.c:28:47: error: dereferencing pointer to incomplete type
foo.c:28:58: error: dereferencing pointer to incomplete type
foo.c:28:69: error: dereferencing pointer to incomplete type
foo.c:28:80: error: dereferencing pointer to incomplete type
foo.c:29:47: error: dereferencing pointer to incomplete type
foo.c:29:58: error: dereferencing pointer to incomplete type
foo.c:29:69: error: dereferencing pointer to incomplete type
foo.c:29:80: error: dereferencing pointer to incomplete type
make: *** [foo] Error 1
Looks like you declare struct stud, but use struct test. Partly my fault I think, as I renamed some stuff to make things more clear (IMO). Fixing that:
Code:
$ make foo
gcc -Wall -ggdb3 -pedantic -std=c99 -O0 -o foo foo.c -lm -lpthread -lrt
foo.c: In function ‘getInput’:
foo.c:19:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat]
Down to one error. Your printf format specifier for n appears to be wrong, it should be %s.
That finally compiles, and I get a seg fault. I notice you're always printing tests[1] instead of tests[i] in getInput.
The real problem you're having is this line:
Code:
tests[i] = malloc(sizeof(tests[i]));
That doesn't quite do what you think. Remember, you have an array of pointers. Thus, tests[i] is a pointer to struct test. That means that sizeof(tests[i]) gives the size of a pointer, not the size of the struct pointed to. You're not allocating enough memory. The internal details of exactly what malloc does can vary from implementation to implementation, but what is probably happening is that malloc is giving you memory for tests[0] and tests[1] in nearly adjacent places in memory, but you're only getting, say, 4 or 8 bytes for each (size of a pointer), instead of the 100+ bytes you should get for the actual structure. This means that when you treat those addresses as actual structures, they overlap, thus modifying tests[1] seems to be modifying tests[0]. This kind of error would be very easily caught with libefence, and valgrind too, I think. Seriously, learn to use them. They're free, not very difficult, and very, very helpful.
The general form is as follows:
Code:
some_type *bar = malloc(sizeof(*bar));
Use the name of the pointer you're allocating an object for, with a single * in front of it. That gives you the size of the dereferenced pointer, i.e. the "object" (though with multiple levels of indirection that "object" may itself be a pointer). So, in summary:
Code:
struct test **tests = malloc(sizeof(*tests) * number_of_tests); // allocates space for number_of_tests pointers to struct test (array elements)
tests[i] = malloc(sizeof(*tests[i])); // allocates space for an actual struct test, not just a pointer to struct test.