int & char pointers

This is a discussion on int & char pointers within the C Programming forums, part of the General Programming Boards category; Why is the following code valid for a char pointer but not for a int pointer? I am confused as ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    int & char pointers

    Why is the following code valid for a char pointer but not for a int pointer?
    I am confused as to why I cant use the same approach for both.

    Code:
    char *y;
    
    y = “This is great”;
    
    printf(“%s”, y);
    But this will fail
    Code:
    int *x;
    
    x = 25;
    
    printf(“%d”, x);

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    %s expects a pointer to char
    %d expects an int
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,538
    Because %s expects a char pointer, which is what y is.
    %d expects an int.

    int x = 42;
    int *px = &x;
    *px = 21;
    printf( "%d", *px );

    Dereferencing a pointer to an int gets an int, which is what %d needs.
    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
    Apr 2008
    Posts
    115

    Great explanation Poseidon - God of the C

    I was really messed up on that but it makes more sense now. This learn C in 21 days book is not all that detailed. Anyways I can see now the difference lies in %s and %d so thanks.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    I still have one more lingering thought which is never good

    So both of these are legal ways to assign values to a pointer?

    y = “This is great”;


    x = 25;


    and I just was wrong in the manner I tried to use printf as you stated?

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    No, but the first one is fine.

    If x is defined as an int *, then you are assigning an address of 25 to x, not the value of 25.

    If you want to point to the address of the 25, then do this:

    int * x ;
    int n = 25 ;
    x = &n ;

    Now, x point to the address of an int.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    115

    So int and char pointers do not work the same at all right?

    Is the below true then?

    p is the pointer
    sorry bout the uppercase


    WHEN USING CHAR POINTERS

    P = ENTIRE STRING

    &P = ADDRESS OF THE POINTER

    *P = 1RST BLOCK OF THE STRING

    *(P + 2) = 3RD BLOCK OF THE STRING



    WHEN USING INT POINTERS

    P = ADDRESS OF THE CONTENTS P IS POINTING TO
    &P = ADDRESS OF THE POINTER ITSELF (INT *P's ADDRESS)

    *P = CONTENTS OF THE POINTER (1 BLOCK OF MEMORY)

    THE POINTER MUST BE INITIALIZED AND VALUES ASSIGNED
    IN A MANNER SUCH AS BELOW:

    int time = 500;

    int *carl2;

    carl2 = &time;
    Last edited by cjohnman; 05-02-2008 at 01:03 PM.

  8. #8
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    char and int pointer are very close - they just differ in "block" size
    for char pointer it is sizeof(char) = 1 byte
    for int pointer it is sizeof(int)

    as additional benefit sequence of chars with nul-terminating char is processed as a C-string by standard library functions desined to process the C-strings

    there is no such assumption for sequense of ints...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 03-10-2008, 11:57 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. Working with random like dice
    By SebastionV3 in forum C++ Programming
    Replies: 10
    Last Post: 05-26-2006, 09:16 PM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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