hey all,
if I do something like this:
that will print the address of the first element in buf (buf[0]), right? just making sure.Code:char buf[100], *p; p = buf; printf("%p\n", p);
thanks in advance!
hey all,
if I do something like this:
that will print the address of the first element in buf (buf[0]), right? just making sure.Code:char buf[100], *p; p = buf; printf("%p\n", p);
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
yes
try to assign a value to *p and then examine the value of buf[0].
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
> 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.
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
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.