Thread: Printing pointers with printf()

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    20

    Question Printing pointers with printf()

    Hi there,

    I have done a program that at a certain point prints a pointer (char*), and it is compiling but it outputs this warning:
    vectorCaracteres.c:42: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘char *’
    Here is line 42:
    Code:
    printf("PONTEIRO -> %d CARÁCTER -> %c\n", vector2[i], *vector2[i]);
    vector2 is declared as an char* vector:
    Code:
    #define TAMANHO 7
    (...)
    char* vector2 [TAMANHO];
    I changed line 42 to the following:
    Code:
    printf("PONTEIRO -> %d CARÁCTER -> %c\n", (int)vector2[i], *vector2[i]);
    and in fact it is working (does not display the warning anymore). Since I don't know if the pointer values displayed are ok, could someone please tell me if I did the right choice or am I going the wrong way?

    If someone could answer me I would be very appreciated :]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    To print a pointer, use
    Code:
    printf("PONTEIRO -> %p CARÁCTER -> %c\n", (void*)vector2[i], *vector2[i]);
    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
    May 2008
    Posts
    20
    ok, many arigatos!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a C Programing Quiz project
    By dilemma in forum C Programming
    Replies: 12
    Last Post: 05-15-2009, 03:35 PM
  2. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 06:54 AM
  3. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 09:23 AM
  4. Replies: 4
    Last Post: 04-01-2003, 12:49 AM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM