format '%p' expects type 'void *', but argument * has type 'int

This is a discussion on format '%p' expects type 'void *', but argument * has type 'int within the C Programming forums, part of the General Programming Boards category; Hi guys! I need to understand why this program, gives this error (although it compiles and does the job well) ...

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

    Unhappy format '%p' expects type 'void *', but argument * has type 'int

    Hi guys!

    I need to understand why this program, gives this error (although it compiles and does the job well)

    Code:
    #include <stdio.h>
    main()
    {
    	char *myString = "Mike";
    	int x;
    	printf("\nThe pointer variable's value is: %p\n", *myString);
    	printf("\nThe pointer variable points to: %s\n", myString);
    	printf("\nThe memory locations for each character are: \n\n");
    	//access & print each memory address in hexadecimal format
    	for ( x = 0; x < 5; x++ )
    		printf("%p\n", myString[x]);
    }
    *example taken from C Programming for the Absolute Beginner, Michael Vine

    I get this error: "format '%p' expects type 'void *', but argument 2 has type 'int'" in line 6 and 11

    Code:
    	printf("\nThe pointer variable's value is: %p\n", *myString);
    
    	printf("%p\n", myString[x]);
    I understand the intention of the author e.g.

    "you can see how the pointer variable myString contains the value of a memory address (printed in hexadecimal format) that points to the first character in the string "Mike", followed by subsequent characters and finally the NULL zero to indicate the end of the string."

    etc.

    However this program has warnings (that I dont really like).

    So could someone explain why we get these errors, how to superpass them and still to explain authors intention?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try ... printf("%p \n", (void*)&myString[x]); if you're looking for it's address...

    If you just want the character.. printf("%c \n", myString[x]);

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,535
    It really depends on what you want to do.

    To print the character
    printf("%c\n", myString[x]);

    To print the decimal value of the character
    printf("%d\n", myString[x]);

    To print the address where a character is stored
    printf("%p\n", (void*)&myString[x]);
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Code:
    #include <stdio.h>
    main()
    {
    	char *colors[3][10] = {'\0'};
    	printf("\nEnter 3 colors seperated by spaces: ");
    	scanf("%s %s %s", colors[0], colors[1], colors[2]);
    	printf("\nYour entered: ");
    	printf("%s %s %s\n", colors[0], colors[1], colors[2]);
    }
    take this one I want scanf-it and print-it ... however I get the same errors How I can superpass same errors here... and do the job well? Thanks Salem and CommonTater already

  5. #5
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    Well you have a 2d array of char pointers. If that is really what you wanted, then you should have to invoke a few malloc() calls. To say the least, the easiest way to fix it is to delete the * from the declaration, and it should work.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by c_lady View Post
    Code:
    #include <stdio.h>
    main()
    {
    	char *colors[3][10] = {'\0'};
    	printf("\nEnter 3 colors seperated by spaces: ");
    	scanf("%s %s %s", colors[0], colors[1], colors[2]);
    	printf("\nYour entered: ");
    	printf("%s %s %s\n", colors[0], colors[1], colors[2]);
    }
    take this one I want scanf-it and print-it ... however I get the same errors How I can superpass same errors here... and do the job well? Thanks Salem and CommonTater already
    Lose the star.... char colors[3][10] = {0};

    Interestingly.... my way will use less space on the stack too...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined reference to.. probably Makefile problem
    By mravenca in forum C Programming
    Replies: 11
    Last Post: 10-20-2010, 04:29 AM
  2. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  3. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  4. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21