Compiler Design in C
-
I have compiler design in C practicals and i have encountered a problem. This goes as : We have to take n number of productions(string) from user. Each production is of different size. I want the array size for each production to be dynamic with exact size as that of the entered string. There should not be any blank elements in array as it would spoil the output. How this can be achieved. I am someone who not so good in C.
-
I have compiler design in C practicals and i have encountered a problem. This goes as : We have to take n number of productions(string) from user. Each production is of different size. I want the array size for each production to be dynamic with exact size as that of the entered string. There should not be any blank elements in array as it would spoil the output. How this can be achieved. I am someone who not so good in C.
You could create an array of pointers if you know the value of n at compile time.
char *strings[50];
Now as you save each string to this array allocate the size.
strings[count] = malloc(sizeof(string) + 1);
strcpy(strings[count], string);If the value of n is not known at compile time, create a pointer to a pointer.
char **strings;
When you get the value of n, allocate the array.
*strings = malloc(sizeof(char*) * n);
Allocation for each string is the same as above. Don't forget to increment the count variable after to store every string.
«_Superman_» I love work. It gives me something to do between weekends.