Okay so I'm having a problem with this. My homework is to take this array of numbers and increment each one by one. Sounded simple at first and then I realized that in the array the address of variables were assigned to an element in the array. My question is..how do I increment a value of address if what I put in is the address?

``````#include <stdio.h>

#define SZ 7

int* a[SZ];
int x, y, z;

void populate() {
x = 1;
y = 2;
z = 3;
a[0] = &x;   //
a[1] = &y;   //Addresses of int variables assigned to array elements
a[2] = &z;   //
a[3] = a[0];
a[4] = a[1];
a[5] = a[2];
a[6] = a[3];
}
void printall() {
int i;
for (i = 0; i < SZ; i++) {
printf("a[%1d]=%1d, ", i, *(a[i]));
}
printf("x=%1d, y=%1d, z=%1d\n", x, y, z);
}

{
//My homework is here.
for(int i = 0; i < sizeof(a); i++)
{
//How do I increment values of addresses?
}
}

int main(int argc, char* argv[]) {
populate();
printall();
printall();
return 0;
}``````
2
Contributors
7
Replies
9
Views
7 Years
Discussion Span
Last Post by Ancient Dragon

I forgot to mention that when I run the program. Using....

{
for(int i = 0; i < sizeof(a); i++)
{ *(a+i) = a + 1; }
}

My results are:

a[0]=1, a[1]=2, a[2]=3, a[3]=1, a[4]=2, a[5]=3, a[6]=1, x=1, y=2, z=3
a[0]=134520864, a[1]=4, a[2]=4, a[3]=134520864, a[4]=4, a[5]=4, a[6]=134520864, x=1, y=6, z=7

//how did that happen? o-o

line 31: The sizeof(a) will not give you the number of rows in the array. What you want is to use the already defined macro SZ that appers on line 3.

How to increment:
(*a)++;

Okay I replaced the method to this:

{
for(int i = 0; i < SZ; i++)
{
(*a)++;
}
}

and my results were....

benji@benji-VirtualBox:~/Desktop/CS47\$ ./hw4a
a[0]=1, a[1]=2, a[2]=3, a[3]=1, a[4]=2, a[5]=3, a[6]=1, x=1, y=2, z=3
a[0]=4, a[1]=4, a[2]=5, a[3]=4, a[4]=4, a[5]=5, a[6]=4, x=4, y=4, z=5

thanks for getting rid of the addresses but the increment is incorrect o.o

line 31: The sizeof(a) will not give you the number of rows in the array. What you want is to use the already defined macro SZ that appers on line 3.

How to increment:
(*a)++;

Okay I replaced the method to this:

{
for(int i = 0; i < SZ; i++)
{
(*a)++;
}
}

and my results were....

benji@benji-VirtualBox:~/Desktop/CS47\$ ./hw4a
a[0]=1, a[1]=2, a[2]=3, a[3]=1, a[4]=2, a[5]=3, a[6]=1, x=1, y=2, z=3
a[0]=4, a[1]=4, a[2]=5, a[3]=4, a[4]=4, a[5]=5, a[6]=4, x=4, y=4, z=5

thanks for getting rid of the addresses but the increment is incorrect o.o

what do you think the results should be? x starts out as 1. Then a[0] is incremented the result is x = 2. a[4] is incremented and the result is x = 3; Now notice that a[6] == a[4] == a[1] == address of x. So when a[6] is incremented the result is x = 4.

what do you think the results should be? x starts out as 1. Then a[0] is incremented the result is x = 2. a[4] is incremented and the result is x = 3; Now notice that a[6] == a[4] == a[1] == address of x. So when a[6] is incremented the result is x = 4.

Sorry I wasn't specific enough =/ but uhm you know how a[0] = 1 at first?
after the increment a[0] = 2 and a[6] = 1 before, after increment should be a[6] = 2.

>>how a[0] = 1 at first

I think this is an assignment to test your understanding of addresses. Since a[0] contains the address of x, any changes to x will affect both x and (*a[0]) because they both point to the same memory location. Now, since a[6] contains the same address as a[0], and the address in a[4] is also the same as a[0], then any one of those variables are changed the new value is reflected in all three pointers.

If you printed out the values of all those address each time any one of them is changed you should see that behavior. For example, increment a[0] then inspect the value of x, a[4] and a[6]. All of them will show the same value becuse they are all pointing to the same memory location.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.