Thread: Pointers problem

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    29

    Pointers problem

    error gave is
    Building slideshow2.obj.
    C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(12): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'int * *'.
    C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(12): warning #2234: Argument 3 to 'printf' does not match the format string; expected 'int' but found 'int *'.
    C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(14): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'int (*)[5]'.
    C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(16): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'int *'.
    C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(18): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'int *'.
    C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(28): warning #2234: Argument 2 to 'printf' does not match the format string; expected 'int' but found 'int *'.
    C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(28): warning #2234: Argument 3 to 'printf' does not match the format string; expected 'int' but found 'int *'.
    C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(9): warning #2116: Local 'piQ' is used but never assigned a value.
    Done.



    Code:
    #include<stdio.h>
    
    
    int main()
    {
    	int *piVPtr;
    	int *piVPtr2;
    	int aiV[5] = { 10, 20, 30, 40, 50 };
    	int iTemp;
    	int *piP, *piQ;
    
    
    	piVPtr = aiV;
    	printf("Address of piVPtr : %d Contents of piVPtr : %d\n", &piVPtr, piVPtr );
    
    
    	printf("Address of aiV[0] : %d\n", &aiV );
    	piVPtr +=2;
    	printf("Address of piVPtr +2 : %d\n", piVPtr );
    	piVPtr +=2;
    	printf("Address of piVPtr +4 : %d\n", piVPtr );
    
    
    	piVPtr2 = &aiV[2];
    	piVPtr = &aiV[0];
    
    
    	iTemp = piVPtr2 - piVPtr;
    
    
    	printf("Content of iTemp : %d\n", iTemp);
    
    
    	piP = piQ;
    	printf("Content of piP : %d piQ : %d\n", piP , piQ);
    
    
    	return 0;
    }
    
    so what is wrong ?
    Someone correct me please, Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    To print addresses, do this
    Code:
    printf("Address of piVPtr : %p Contents of piVPtr : %p\n", (void*)&piVPtr, (void*)piVPtr );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    29
    ok.. Thank you..

    I tried to change most of it . However it still give an error saying ''C:\Users\Eric\Documents\Pelles C Projects\C6\slideshow2.c(9): warning #2116: Local 'piQ' is used but never assigned a value.''

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It's exactly what the warning says. 'piQ' is declared, and used - but is never assigned a value.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Location
    Athens , Greece
    Posts
    357
    Hello merry christmas !!!

    Code:
    #include<stdio.h>
     
     
    int main()
    {
        int *piVPtr=NULL;
        int *piVPtr2=NULL;
        int aiV[5] = { 10, 20, 30, 40, 50 };
        int iTemp;
        int *piP, *piQ;
     
     
        piVPtr = aiV;
        printf("Address of piVPtr : %p Contents of piVPtr : %p\n", &piVPtr, piVPtr );
     
     
        printf("Address of aiV[0] : %p\n", &aiV );
        piVPtr +=2;
        printf("Address of piVPtr +2 : %p\n", piVPtr );
        piVPtr +=2;
        printf("Address of piVPtr +4 : %p\n", piVPtr );
     
     
        piVPtr2 = &aiV[2];
        piVPtr = &aiV[0];
     
     
        iTemp = piVPtr2 - piVPtr;
     
     
        printf("Content of iTemp : %d\n", iTemp);
     
     
        piP = piQ;
        printf("Content of piP : %p piQ : %p\n", piP , piQ);
     
     
        return 0;
    }
    Here is what you want to have without compiler warnings. So this code is not suitable though.

    You really need some key points in order to use pointers with the good way. First of all you don't initialize the pointer piQ

    assuming the line :

    Code:
    piP = piQ;
        printf("Content of piP : %p piQ : %p\n", piP , piQ);
    Where both piQ and piP do you think that they point? THe answer is you don't. That is dangerous you must initialize a pointer with a valid address before you use it.
    The second one is that you should use %p instead of %d when you want to print an address for example if you have a pointer like:

    Code:
    int  x=1 , *p = &x;
    now p points to x... and *p is an alias for x since *p=x=1 . But p is not an integer value is an address of that integer value.

    The third one is.... when you try to write a program you really need to give good names in the variables.... for example what is piVptr and aiV names? I can't understand with the first sight

    for now it is not bad... but if you have a program which implements an algorithm you really give suitable names to variables.

    For example arr for 1D array and arr2d for a 2D array

    Mr.Lnx
    Last edited by Mr.Lnx; 12-23-2012 at 09:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointers
    By aakashjohari in forum C Programming
    Replies: 10
    Last Post: 01-19-2011, 01:51 PM
  2. a problem with pointers
    By carter88 in forum C Programming
    Replies: 1
    Last Post: 12-02-2009, 06:36 AM
  3. Problem with pointers
    By jcafaro10 in forum C++ Programming
    Replies: 4
    Last Post: 01-27-2009, 02:36 PM
  4. Problem with malloc and pointers to pointers
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 03-29-2008, 06:03 PM
  5. A problem with pointers
    By vsla in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 04:14 AM