Printing pointers with printf()

This is a discussion on Printing pointers with printf() within the C Programming forums, part of the General Programming Boards category; Hi there, I have done a program that at a certain point prints a pointer (char*), and it is compiling ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 05: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, 08:23 AM
  4. Replies: 4
    Last Post: 03-31-2003, 11:49 PM
  5. Azbia - a simple RPG game code
    By Unregistered in forum Game Programming
    Replies: 11
    Last Post: 05-03-2002, 06:59 PM

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