Well your struct is defined as:
struct cp
So you should return a struct cp, not a struct chiplib? I don't see how this isn't a syntax error.
Here's another problem which tells me that you don't understand what an array is in C:
return &(ctemp[0]);
So you want to return the pointer that points to the first element in the array, right?
So you just say:
return ctemp;
When you have a variable that is an array, using the name of the variable without an index means that it's a pointer to the first element in the array.
Finally, and MOST important, your code will produce a seg-fault, you cannot return arrays or pointers from functions without malloc'ing them...do you want me to attempt to explain why you have to malloc pointers and arrays?
Edit: OK, here goes:
You cannot return pointers (arrays are pointers) from functions because after the function returns the memory used to create the array is recycled and therefore no longer accessible...that is after the function returns the actual physical memory where your array was created is likely overwritten with the next instruction in the program. So you get a seg-fault because you try to access memory that you do not have privilege to access (i.e. you might modify the stack and if it allowed you to do that, then your code could become inconsistent and at best would just produce non-sense and at worst destroy your OS).
OK, you have to understand how memory is allocated in C (and most other programming languages). There is a reason that ANSI C only allows you to declare variables @ the beginning of a block. This is because when a block is entered memory is allocated for all declared variables, when the block exits that memory is "returned", meaning, abstractly, that it doesn't exist anymore.
Example
void foo(void){
int i = 10;
char *myChar;
{
char string = {"this", "wont'", "be", "accessible"};
myChar = string;
}
printf("%s", myChar);
}
This should also produce a seg-fault, although string is defined inside the function, it is defined in an inner block inside the function. So when I do:
myChar = string;
I make myChar take the value of the pointer to the first element in the char array string. However, that memory address is basically bad as soon as the block exits. Hopefully this illustrates that more general rule, that it's not that functions are special it's that functions define a block of code and it's the block that's important.
So what do you do? Well if you want the memory to be accessible outside of the block, then you need to tell the program to allocate somewhere outside of the block, malloc is the way to do this and we usually call this the heap, which is exactly what it sounds like there is a heap of memory that is used for dynamic memory (i.e. what is contains changes asynchronously).
So you just need to malloc whatever it is that you return:
If you want an array of structs then:
struct chiplib* = malloc(475 * sizeof(struct chiplib));
Whatever the size of the array you want, you multiply by the sizeof the struct...sorry I was taught not to use typedef's, so I'm more familiar with just using the struct.
Be aware, that since you are telling the program to allocate memory outside of the function, YOU the programmer are now in charge of it, so if you lose it (say by pointing the pointer to a different place) then you can't get the memory back. This is a memory leak, where you have memory that nothing points to and is thus inaccessible, honestly memory leaks aren't important except for large long-running programs, and they are very difficult to find and hunt down even with tools such as valgrind.
I am more familiar with Object oriented programming so I tend write C code like that. So usually I would create a function (akin to new) that allocates memory for a new struct and also create a destructor that frees the memory. The reason the destructor is necessary is that you may have pointers inside of your struct so you want the destructor to make sure that any dynamic memory allocated at the creation of the struct is also freed.
I hadn't seen that you DID use malloc...however it appears (we can't see the whole thing) that you didn't malloc the array of structs. Your main method won't produce a seg-fault, so are you running something different after:
getchipinfo(...)?
If you only malloc'd one struct then trying to access say the 2nd or third:
ctemp[1] or higher will produce a seg-fault, but trying to access ctemp[0] should be fine.
Fix:
You should have ONE pointer instead of two (the array and pointer):
struct cp *ctemp = malloc(475 * sizeof(struct cp));
if(ctemp == NULL){
perror("malloc error");
return NULL;
}
while(...){
//do everything you need to do just like it was an array:
strcpy(ctemp[i].resolu, strtok(NULL,","));
...
++i;
}
return ctemp;