# Thread: Lost in structures and pointers

1. ## Lost in structures and pointers

The situation is that I am going to have a couple of typedef struct's, something like (of course more complicated

Code:
```typdef struct {
double     *arrPtr1;
double     *arrPtr2;
} arrays;

typedef struct {
...
...
...
arrays      *array;
...
...
} structure;```
So there's structure which includes a pointer to an arrays struct, which has pointers inside of it.

I'm going to have to dynamically supply memory to the arrays through malloc, and then funnel a calculation into these arrays.

I'm unsure of the mechanics accessing these arrays for the malloc and subsequent use, and I haven't worked much with structs so hoping for some advice. Just trying to lay it out for myself at the moment.

Thanks.

2. I suggest that you approach this fby experimenting in steps:

1. Given this struct definition:
Code:
```typedef struct foo
{
int x;
} foo;```
Write a program that defines an object of foo and uses its x member.

2. Given this pointer:
Code:
`int *x;`
Write a program that uses malloc to allocate memory for an array of ints, and store the resulting pointer value in x. Make use of the dynamic array through x, then call free when you are done.

3. Combine what you understand from steps 1 and 2 to do what you originally set out to do.

3. The first two I can do all right, the following came out clean in Valgrind and produced expected results. I suppose I'm lost on the logic of what I'm trying to do in the end.

Code:
```int main()
{
typedef struct foo
{
int x;
} foo;

int *x, *xstart;

foo integer;

integer.x = 5;

printf("should get 25:  %d\n", 5*integer.x);

x = (int *) calloc(10, sizeof(int));
xstart = x;

for(x = xstart; x < xstart + 10; ++x)
{
*x += 2;
printf("%d\n", *x);
}

free(xstart);
return 0;
}```
So I naively tried something like:
Code:
```int main()
{
typedef struct foo
{
int *x;
} foo;

foo array;
int *start;

array.x = (int *) calloc(10, sizeof(int));
start = array.x;

for(array.x = start; array.x < start + 10; ++(array.x))
{
array->x = 1;
printf("%d\n", array->x);
}

free(start);
return 0;
}```
and wound up with a disaster.

4. In the "disaster" case, I think only the code near for loop needs to be fixed:

Code:
```    for(start = array.x ; start < array.x + 10; ++start)
{
*start = 1;
printf("%d\n", *start);
}

free(array.x);```

5. That works quite well! I was maybe a bit closer than I thought.

Thanks!

6. Originally Posted by shallowbay
That works quite well! I was maybe a bit closer than I thought.
Closer than I thought also. All you really needed to change was two lines in the for loop:

Code:
```        *(array.x) = 1;
printf("%d\n", *(array.x));```
I used a more "conventional" approach by using the local variable start to iterate through the data in array.x. What your program did was copy the pointer from array.x into start, and with the fix shown in this post, used array.x to iterate through the array, then freed the memory using the copy of the pointer stored in start, but this is less "conventional".

7. I like they way you did it better as it's certainly more natural.

Code:
```int main()
{
typedef struct foo
{
int *x;
} foo;

foo array;
int *start;

array.x = (int *) calloc(10, sizeof(int));
start = array.x;

for(array.x = start; array.x < start + 10; ++(array.x))
{
*(array->x) = 1;
printf("%d\n", *(array->x));
}

free(start);
return 0;
}```
I had actually tried this first, but I get an error: invalid type argument of '->' (have 'foo') at which point I tried switching to just using array->x.

8. Originally Posted by shallowbay
... *(array->x) ...
I corrected my previous post, that should have been *(array.x):

Code:
```        *(array.x) = 1;
printf("%d\n", *(array.x));```
but as mentioned before, you should leave array.x alone and use start to iterate through the array of number, and since you're using "start" to iterate through array.x, you might want to choose a better name, perhaps something like "intptr" (integer pointer), or "ptr2int".

Popular pages Recent additions