size of a char pointer (*)

This is a discussion on size of a char pointer (*) within the C Programming forums, part of the General Programming Boards category; Okay, I had this tid-bit of code and this output: Code: char *name; fgets(name, sizeof(name), stdin); printf("name = %s\n", name); ...

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    size of a char pointer (*)

    Okay, I had this tid-bit of code and this output:
    Code:
    char *name;
    fgets(name, sizeof(name), stdin);
    printf("name = %s\n", name);
    When I entered the string, "Garfield", the output was:
    "Gar" (and this is NOT including the \n char). Why is this? Why not the whole string? What is the size of this pointer anyway and what is the problem with this code? Thanks...

    --Garfield
    1978 Silver Anniversary Corvette

  2. #2
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    since it was a 32-bit ptr, you get 4 bytes... hence Gar...
    hasafraggin shizigishin oppashigger...

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    how do you make it more bytes?
    1978 Silver Anniversary Corvette

  4. #4
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284

    Re: size of a char pointer (*)

    Originally posted by Garfield
    Okay, I had this tid-bit of code and this output:
    Code:
    char *name;
    fgets(name, sizeof(name), stdin);
    printf("name = %s\n", name);
    When I entered the string, "Garfield", the output was:
    "Gar" (and this is NOT including the \n char). Why is this? Why not the whole string? What is the size of this pointer anyway and what is the problem with this code? Thanks...

    --Garfield

  5. #5
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    pinko_liberal, why did you just post a reply that just consists of a quote from my message? Can anyone answer my question? How can I allocate more bytes for the char *? Thanks...

    --Garfield
    1978 Silver Anniversary Corvette

  6. #6
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    one of the differnces between arrays and pointers is wrt to the sizeof operator
    while
    if you declare
    char a[100];
    sizeof(a) will be 100
    so you in this case do
    if you declare
    char *a;
    sizeof(a) will depend on your system , it will be usually around 4 bytes
    just declaring
    char *a; doesnt do anything ,a points to a randomly located memory location , accessing which may cause segment fault , you must allocate memory for it,
    if you think that 100 is sufficient for your purpose
    a=malloc(100);
    then
    fgets(a,100,stdin);


    char *fgets(char *s, int n, FILE *stream);
    char *s the string that will hold the result.
    int n the maximum number of characters to read.
    FILE *stream is an already existing stream.




    Originally posted by Garfield
    how do you make it more bytes?

  7. #7
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    I am sorry , in the above case it should be
    fgets(a,99,stdin)
    I forgot about the trailing '\0'

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Oh, I see. So, I have to allocate the neccessary memory that I want (if I don't want 32 bits - 4 bytes) to be explicit. Thanks!

    --Garfield
    1978 Silver Anniversary Corvette

  9. #9
    Unregistered
    Guest
    char name[20];
    fgets(name, sizeof(name), stdin);
    printf("name = %s\n", name);
    return 0;

    buffer being passed to the fgets is not big enough to store the
    whatever u entering at command prompt

    venu
    venu_m_j@yahoo.com

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,834
    > fgets(a,99,stdin)
    > I forgot about the trailing '\0'
    You were right first time - it should be 100 for your example.
    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.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Garfield
    Oh, I see. So, I have to allocate the neccessary memory that I want (if I don't want 32 bits - 4 bytes) to be explicit. Thanks!

    --Garfield
    With a pointer, you ALWAYS must allocate memory, no matter the size.

    When you do this:

    char * charPtr;

    you tell the program to allocate 4 bytes to store the ADDRESS of a string. The actual memory that the string will eventually go in is NOT yet allocated, and charPtr contains a RANDOM (aka garbage) address. You need to allocate memory to make this address point to an area of memory that has been allocated, otherwise you're writing your string into an unallocated section of memory, which isn't really a good idea (read: very very bad).

    Also, when you allocate memory, you must free this memory when you're done with it.

  12. #12
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, I hate to beat a dead horse, but I'm still a little dazed and confused. I understand exactly what you are telling me about allocating, 4 bytes, etc. But what I don't understand can be found here in this code:
    Code:
    char *string;  /* This is legal, by my compiler */
    string = "Garfield";
    
    /* But if I would do this, then it would flag an error */
    int *int_ptr;
    *int_ptr = 4; /* This flags an error */
    I know that I would have to allocate memory for the int_ptr to use it, but why wouldn't you have to do that for the char *? What I'm trying to say is why don't you treat char * and int * as the same way?

    --Garfield
    1978 Silver Anniversary Corvette

  13. #13
    Unregistered
    Guest
    A pointer, regardless of 'char*', 'int*' 'struct mystruct *', is the same size. A pointer is a variable that stores the memory address of whatever type it is designed to point at. Thus, a "char pointer" is designed to point-at a character. Since it is a pointer to a memory location, it stores the value of that memory location (it's address).
    Code:
    #include <stdio.h>
    
    int main ( void ) {
    
        printf("char*  = %d bytes", sizeof ( char* ) );
        printf("int*   = %d bytes", sizeof( int* ) );
        printf("float* = %d bytes", sizeof( float* ) );
        return 0;
    }
    See? This is different than when you're comparing the 'sizeof' the actual variable.

    To use a pointer, you declare one:

    char *c; //character pointer, currently points to random memory address

    Give it a value. Always do this first. You _have to_ initialize a pointer before using it, or bad things happen:

    c = NULL;

    You can generally do this in one shot:

    char *c = NULL;

    Now, if we have this:

    char *c = NULL;
    char array[10] = "Hello Bob";

    We can do something like:

    c = array;

    Which sets c to store the value of the memory address that the array holds. (Maybe not the best explanation, but basicly, we're "pointing at" our array now.)

    Now you can do something like:

    puts( c );

    Which should print "Hello Bob". (Less quotes.)

    Quzah.

  14. #14
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    Originally posted by Garfield
    Okay, I hate to beat a dead horse, but I'm still a little dazed and confused. I understand exactly what you are telling me about allocating, 4 bytes, etc. But what I don't understand can be found here in this code:
    Code:
    char *string;  /* This is legal, by my compiler */
    string = "Garfield";
    
    /* But if I would do this, then it would flag an error */
    int *int_ptr;
    *int_ptr = 4; /* This flags an error */
    I know that I would have to allocate memory for the int_ptr to use it, but why wouldn't you have to do that for the char *? What I'm trying to say is why don't you treat char * and int * as the same way?

    --Garfield
    This is because of how strings are treated by the compiler.

    When you say:

    string = "Garfield";

    you tell the compiler to include, in the data section of the program, the literal string "Garfield\0". Like all the data section, when you run the program, it is loaded into memory. So, the command string = "Garfield"; tells string to point at the memory location where the string "Garfield\0" is sitting in your program's data section. You need not allocate memory for "Garfield\0" because this is automatically allocated, and loaded into memory, when your program runs.

    By contrast, a numeric literal is not loaded into the data section, it is used as an immediate operand. So,

    *int_ptr = 4;

    will segfault you because you never allocated memory for the int.

    In general, memory is allocated automatically if:

    1) You create a variable or an object.
    2) You use a string literal

    But it is not allocated if:

    1) You create a pointer to a primitive or object. (Memory for the POINTER will be allocated automatically, but the memory it points AT is not allocated).

  15. #15
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Thanks everybody! I am no longer confused.

    --Garfield
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 10:42 PM
  2. An exercise in optimization
    By Prelude in forum Contests Board
    Replies: 10
    Last Post: 04-29-2005, 04:06 PM
  3. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 12:57 PM

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