Thread: obtaining a variable's address

  1. #1
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211

    obtaining a variable's address

    hey all,
    if I do something like this:

    Code:
    char buf[100], *p;
    p = buf;
    
    printf("%p\n", p);
    that will print the address of the first element in buf (buf[0]), right? just making sure.


    thanks in advance!
    Last edited by Bleech; 09-06-2006 at 12:54 AM.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  2. #2
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    275
    yes

    try to assign a value to *p and then examine the value of buf[0].

  3. #3
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    thanks fnoyan, tried this code and it prints "a":

    Code:
    #include <stdio.h>
    
    int main(void)
    {
    	char buf[100], *p;
    	p = buf;
    	*p = 'a';
    
    	printf(buf);
    
    	return 0;
    }

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > tried this code and it prints "a":
    Consider yourself somewhat lucky then.

    There is no obvious sign of where the \0 to end the string came from

    > printf(buf);
    This is a really bad habit you need to get rid of.
    Never pass user data as a format control string. Any % characters which get in there will cause you BIG problems.
    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.

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    really? I didn't know that . what can happen if I print a string containing format specifiers like that? not that I want to do it, I am just curious, since you said its a "really" bad habit.

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Send Mac Address From Client to Server
    By Lieyza197 in forum C Programming
    Replies: 2
    Last Post: 05-27-2009, 09:58 AM
  2. Address of variables?
    By alex1067 in forum C Programming
    Replies: 1
    Last Post: 05-07-2008, 10:47 PM
  3. import address table (IAT)
    By George2 in forum C++ Programming
    Replies: 5
    Last Post: 02-20-2008, 08:01 AM
  4. HelpMePlease
    By Prncess100 in forum C++ Programming
    Replies: 6
    Last Post: 12-11-2002, 02:02 PM
  5. reading hexadecimal variables
    By juhigarg in forum C++ Programming
    Replies: 7
    Last Post: 11-06-2001, 11:10 PM