-
test question
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; }
i can't compile it, i get an error:
"14[Warning] assignment makes integer from pointer without a cast "
and another Q if you will:
if i change :
Code:
printf("10) %d",ptr[2]-arrayInts[2]); newline;
to:
Code:
printf("10) %d",ptr[2]-arrayInts[13]); newline;
what will I get...?
can a pointer change the memory location of a whole array ?
thanx a lot !
-
This compiles and runs for me, dunno if it outputs what you want:
Code:
#include <stdio.h>
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\n", ptr);
printf("2) %d\n", *ptr);
printf("3) %d\n",*ptr++);
printf("4) %d. ",&arrayInts[1]);
ii=*(++ptr);
jj=*(ptr++);
printf("\n5) %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("Thats it. \nGood luck\n\n");
return 0;
}
-
Your change would reference a position outside the array, so it's indeterminate, may even crash the program.
No. You could use a pointer in moving the contents of an array, but the array would still
have the original address, only its contained values would have moved.
The program would still have "arrayInts" pointring to the same location.
Changing a pointer changes nothing's location - not even the pointer's - only its value, an address, and therefore the stuff that it points to is somewhere else and probably different.
Pointers are only stored addresses and can be used to access something at the stored address or changed to point to somewhere else.
Got it?
-
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