2d array, using calloc in C

Posted by BigBirdy on Stack Overflow See other posts from Stack Overflow or by BigBirdy
Published on 2010-04-19T00:47:42Z Indexed on 2010/04/19 0:53 UTC
Read the original article Hit count: 507

Filed under:
|
|
|
|

Hi,

I'm trying to create a 2D array of chars to storage lines of chars. For Example:

lines[0]="Hello";
lines[1]="Your Back";
lines[2]="Bye";

Since lines has to be dynamically cause i don't know how many lines i need at first. Here is the code i have:

int i;
char **lines= (char**) calloc(size, sizeof(char*));

for ( i = 0; i < size; i++ ){
lines[i] = (char*) calloc(200, sizeof(char));
}

for ( i = 0; i < size; i++ ){
free(lines[i]);
}

free(lines);

I know that each line can't go over 200 chars. I keep getting errors like "error C2059: syntax error : 'for'" and such. Any ideas of what i did wrong?

© Stack Overflow or respective owner

Related posts about c

    Related posts about c++