I want to iterate through null terminated structure array. While loop works fine, but when I try to access any structure fields I get "segfault" in response. What am I doing wrong?
PS I know that it is possible to determine array size and use simple "for" construction. I just want to figure out the source of the problem.
PSS Ok, if array initialization is wrong - how to do it correctly?
#include <stdio.h>
#include <stdlib.h>
typedef struct demo
{
int a;
int b;
int c;
} demo;
demo * fieldDefinitions[] =
{
{ 1 , 1, 1 },
{ 2 , 2, 2 },
{ 3 , 3, 3 },
NULL
};
int main()
{
demo ** ptr = fieldDefinitions;
printf( "Array: %d \n", &fieldDefinitions );
while ( *ptr != NULL )
{
printf( "ptr: %d \n", ptr );
printf( "ptr: %d \n", (**ptr).a ); // <--- problem here
ptr++;
}
return 0;
}
demo *fieldDefinitions[] = { &(demo){ 1, 1, 1 }, ..., 0 };.