Thread: test question

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    6

    Question 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 !

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    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;
    }

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    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?

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about binary trees and files
    By satory in forum C Programming
    Replies: 9
    Last Post: 03-06-2006, 06:28 AM
  2. Why is my program freezing?
    By ShadowMetis in forum Windows Programming
    Replies: 8
    Last Post: 08-20-2004, 03:20 PM
  3. Question About Linker Errors In Dev-C++
    By ShadowMetis in forum C++ Programming
    Replies: 9
    Last Post: 08-18-2004, 08:42 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. test scores
    By ucme_me in forum C Programming
    Replies: 4
    Last Post: 11-14-2001, 01:48 PM