Thread: pointer printing

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

    Unhappy pointer printing

    Code:
    #include <stdio.h>
    main(){
    	int a[20];
    	int i;
    	for (i=0; i<20; i=i+2) {
    		a[i]=i;
    	}
    	for (i=0; i<20; i=i+2) {
    		printf("%d", a[i]);
    	}
    	int *p;
    	p=a;
    	
    	int dd;
    	dd=*(p+3);
    	
    	int gg;
    	gg=a[3];
    	
    	printf("\n %d", dd);
    	printf("\n %d", gg);
    
    }
    I would like to print the third element of the array with pointers but I am unable to do it... even with simple address operator why?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Try printf with %p instead of %d.

    Edit: Just read again what you were ACTUALLY trying to do. Sorry.

    So, if you want to print the address of the third element use what I indicated above.

    If you want to print the value contained at that address use %d.

    NOTE, that your implementation leaves all uneven indexes of the array uninitialized. Only a[0],a[2],a[4]... will have correct values, so a[3] will probably contain garbage.
    Last edited by claudiu; 06-10-2010 at 02:50 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by claudiu View Post
    Try printf with %p instead of %d.
    ...What?

    Define "not working". Also note that the 3rd element is index 2.
    And make your main function return int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Elysia View Post
    ...What?

    Define "not working". Also note that the 3rd element is index 2.
    And make your main function return int.
    Just edited my post, I thought she was trying to print the address of the third element.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Code:
    #include <stdio.h>
    main(){
    	int a[5]={1,4,7,10,13};
    	int i;
    	
    	printf("\n %d \n", a[10]);
    	for (i=0; i<5; i++) {
    		printf("%d", a[i]);
    	}
    	
    	int *p;
    	int *q;
    	p=a;
    	p=*(a+2);
    	printf("\n %d", *p);
    	q=*(a+3);
    	if (p<q) {
    		printf("\n TRUE");
    	}
    }
    Why I have bad access here? (EXC_BAD_ACCESS) --> printf("\n %d", *p);
    If I write like this no problem printf("\n %p", p);

    please let me know

  6. #6
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well, clearly, a and *(a+2) are two different types no? So you are assigning to type int* two different types. One of them is wrong, I'll leave it up to you to figure out which one.

    Also, p<q compares two addresses, the one pointed to by p and the one pointed to by q. Not exactly what you want is it?

    Remember:

    A POINTER IS AN ADDRESS. The dereferencing operation on a pointer type (i.e. *ptr) is the VALUE contained AT THE ADDRESS the pointer POINTS TO.
    Last edited by claudiu; 06-10-2010 at 03:40 AM.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Main must return int.
    Thank you.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Elysia --> what do you mean with "Main must return int"? return 0 ... or?

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    main doesn't have a return type in your code. It must return int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by Elysia View Post
    main doesn't have a return type in your code. It must return int.
    I think main() is allowed in C89, however you are right in that everyone should adhere to the new standard.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's considered bad practice to use implicit return types. C99 goes as far as banning them altogether.
    Therefore, don't use them. That's why I tend to tell newbies that they must make main return int.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with printing instruction pointer
    By Subsonics in forum C Programming
    Replies: 0
    Last Post: 10-11-2009, 03:33 PM
  2. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. printing the contents the pointer points to
    By cblix in forum C Programming
    Replies: 8
    Last Post: 01-27-2006, 10:10 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM