Thread: pointer vs array...

  1. #16
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I used %u so it's easy to manipulate.
    Your preference for decimal is showing.

    Can u please explain the meaning of "undefined behaviour"?
    The short answer: there is a format %p that you are supposed to use to print an address. Consequently, no other format needs to be implemented so that it works.

    Longer answer: One thing that makes %u wrong is that you are assuming the unsigned integer type and the pointer type will always be the same size. You are also assuming that pointer types are all the same size. Many implementations are accommodating because the assumptions you make are true on your home computer, running 32-bit code. Neither of these things are guaranteed to be true so your results will be suspect on platforms different from your home computer. Your choice of integer format is also completely arbitrary. Nothing makes %u more suitable than %d for replacing the proper choice, %p.

    1. This printf statement will print the address of char 'T' of string "This" and variable 'charr' will be stored in symbol table to link it when variable is used.
    2. In this when chptr will be used it will bind the address where address of string "this" is stored. and chptr will take 4 byte to store the starting address of "this".
    You are not even printing what the comments say you are printing. You are printing the addresses of the location of the pointers themselves instead of the pointers' values. Printing the pointers' values looks like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char charr[]="This";
        char *chptr="This";
        printf("%p\n", (void*)charr);
        printf("%p\n", (void*)chptr);
        return 0;
    }
    There are also things that make no sense whatsoever in your comments. What is being passed to printf is a pointer char* cast to void* so that will take up stack space in the printf function. The initial values of the array charr, and the literal string used to initialize chptr are also stored somewhere, so that will take up space. And finally, gcc -- along with any compiler -- does not allocate memory for any program. The OS is in charge of that, when the program is executing.
    Last edited by whiteflags; 11-11-2012 at 08:04 PM.

  2. #17
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by dojha00 View Post
    Can u please explain the meaning of "undefined behaviour"?
    In simple terms, it means your code can do anything. It might produce the results you intend. It might reformat your hard drive. Any results you can imagine are permitted because the C++ standard says (in effect) "anything that happens is permitted".

    Quote Originally Posted by dojha00 View Post
    Let me make clear what i m trying to say in comments:
    I'm happy to let you make things clear. When do you plan to do so?

    Sorry, but you're including a bunch of extraneous information that is not really relevant to what the code is doing. And the relevant parts of what you say are incomplete.

    Quote Originally Posted by dojha00 View Post
    Now if still it's wrong then make me correct..
    Both of your printf() statements yield undefined behaviour, because you are using "%u" to print out pointers. No more needs to be said. Once code has undefined behaviour, it is pointless saying anything else about it. Because, short of changing the code, there is no way to make the behaviour defined.

    Even if you corrected that (using %p to print out pointers as whiteflags suggested, rather than %u, assuming your compiler supports the 1999 C standard or later) then you need to understand that pointers have two attributes: value (which might be printed like an address using the %p format) and type.

    In your code, &charr has type "pointer to array of 5 char". It therefore cannot point at a single char although its value, if you print it out, will be the same value you'd get by printing out the address of the 'T' (i.e. the value is the same, the type is different). Similarly, &chptr has type "pointer to pointer".

    Your observation about variable charr stored in symbol table is compiler specific at best, and irrelevant to describing what the code does. Similarly, your observation that "chptr" will take 4 byte ..." is not guaranteed (different compilers will make chptr different sizes) and is also not relevant to describing what the code does.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #18
    Dweeb dojha00's Avatar
    Join Date
    Feb 2012
    Location
    Global
    Posts
    23
    Thanks grumpy and whiteflags for making me more clear about using %u and %p for printing addresses.
    Btw when i talk about size, i thought about gcc 32-bit compiler(gcc version 4.6.3) which i have described in my comments written inside program and for making it easy to understand i did not focus on type of pointer(which has become an important issue now).
    What is being passed to printf is a pointer char* cast to void* so that will take up stack space in the printf function.
    Suppose if i m not casting it to void* then from where will it take place (heap??)?
    If u can provide me some more useful link about this then it will be better ...
    Btw i m trying to google it and know more..
    What is life??

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Suppose if i m not casting it to void* then from where will it take place (heap??)?
    No matter what type it is the pointer will be passed to printf so it will take up space. Just like any other data you pass in to printf will take up stack space.

    Printf implementations at some point have to use a C feature called variable argument lists: Variable Argument Lists in C and C++ - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 01-28-2010, 02:44 AM
  2. Casting from a 1D array pointer to a scalar pointer
    By komkamol in forum C Programming
    Replies: 8
    Last Post: 09-25-2009, 01:44 AM
  3. pointer to pointer that points to a char array
    By steve1_rm in forum C Programming
    Replies: 2
    Last Post: 01-14-2009, 12:03 AM
  4. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  5. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM