Thread: strange error! with pointer

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    Question strange error! with pointer

    #include <stdio.h>

    Code:
    int main(){
    	int a[10];
    	int *pa = a; //this is OK but...
    	*pa = a; //this is EXC_BAD_ACCESS 
    	/* (EXC_BAD_ACCESS is telling you is that you are 
    	 attempting to reference a pointer that is no longer valid.
    	*/
    	//WHY?
    }
    as you can see above almost I have the same definitions but one doesn't give me an error and other one it does... WHY?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    This:
    Code:
    int *pa = a; //this is OK but...
    Is the same as this:
    Code:
    int *pa;
    pa = a;
    This:
    Code:
    	*pa = a; //this is EXC_BAD_ACCESS
    Isn't assigning a pointer, it's dereferencing a variable and assigning a value to where you're pointing.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    27
    *pa = a; //this is EXC_BAD_ACCESS
    Ritly said by quzah, depending on your compiler, that value can be 0 (or some other garbage value out of program memory), which means NULL. So attempting pointer which is not valid.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    But could you please explain why I get this warnings here?

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    main(){
    	//define two strings: a and b
    	char a[]="Cool";
    	char b[]="things";
    	//define a pointer 
    	char *ptrA;
    	ptrA=a;
    	char d;
    	d=*(ptrA+2);
    	int *pp;
    	int zz=4;
    	pp=&zz;
    	*pp=33;
    	
    	
    	printf("%s", ptrA);
    	printf("\n %p", ptrA);
    	printf("\n %p", ++*ptrA); // format '%p' expects type 'void *', but argument 2 has type 'int'
            printf("\n %p", *(ptrA+2)); // format '%p' expects type 'void *', but argument 2 has type 'int'
    	printf("\n %p", d); // format '%p' expects type 'void *', but argument 2 has type 'int'
    	printf("\n %c", d);
    	printf("\n %d", zz);
    }

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    27
    Try to read the help. According to warnings, printf() takes void*, where as you are providing it int.
    I hope you have some knowledge of pointers. ptrA is a pointer, and *ptrA is value pointer is pointing to.

    with format specifier %p, you need to provide a void* pointer to printf(), not any value.
    How to fix your issues.
    Use printf() with format specifier %d for integer, %c for char, and many more.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing argument from incompatible pointer type
    By bhdavis1978 in forum C Programming
    Replies: 5
    Last Post: 03-17-2010, 12:42 PM
  2. How did you master pointers?
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-17-2006, 08:23 PM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM