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);
}
void add1each()
{
//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();
add1each();
printall();
return 0;
}