Originally posted by dudinka
Code:
hey guys, could someone help me with the output of this program:
Code:
#include <stdio.h>
#define newline {putchar(’\n’);}
int main() {
int *ptr,ii,jj;
int arrayInts[] = {4,6,8,9,10,12,14,15,16,18};
ptr = arrayInts;
printf("Hello.");
printf("\nplease write me the output of this program\n");
printf("1) %d", ptr); newline;
printf("2) %d", *ptr); newline;
printf("3) %d",*ptr++); newline;
printf("4) %d. ",&arrayInts[1]);
ii=++ptr; jj=ptr++; newline;
printf("5) %d.",ii-jj); newline;
printf("6) %d.", *ptr); newline;
printf("7) %d.",*(ptr+2)); newline;
printf("8) %d.", *(arrayInts+2)); newline;
ptr-=1;
printf("9) %d.", *(ptr+=4)); newline;
printf("10) %d",ptr[2]-arrayInts[2]); newline;
printf("That’s it. \nGood luck\n\n");
return 0; }
please format you code properly, it very horrible to read. well the formated program looks like this
Code:
#include <stdio.h>
#define newline {\
putchar(’\n’);\
}\
int main()
{
int *ptr,ii,jj;
int arrayInts[] = {4,6,8,9,10,12,14,15,16,18};
ptr = arrayInts;
printf("Hello.\n");
printf("\nplease write me the output of this program\n");
printf("1) %d\n", ptr);
printf("2) %d\n", *ptr);
printf("3) %d\n",*ptr++);
printf("4) %d.\n",&arrayInts[1]);
ii=++ptr;
jj=ptr++;
printf("The value of ii is - > %u \nAnd the value oif jj -> %u\n",ii,jj);
printf("5) %d.\n",ii-jj);
printf("6) %d.\n", *ptr);
printf("7) %d.\n",*(ptr+2));
printf("8) %d.\n", *(arrayInts+2));
ptr-=1;
printf("9) %d.\n", *(ptr+=4));
printf("10) %d\n",ptr[2]-arrayInts[2]);
printf("That’s it. \nGood luck\n\n");
getchar();
return 0;
}
well, u get that error b'cose u are assigning the address to the integer variable rather than the value. if u do that u get the address the of the memory location where the pointer is pointing. run the above code see what the value is assigned to the ii and jj variable. so in order to achive for the proper ouput change that bit code to this.
Code:
ii=*(++ptr);
jj=*(ptr++);
i am just dereferencing the pointer to get the value . now u dont get any warning
s.s.harish