User forums > Embedded development

identical strings are showing same memory address, why?

(1/1)

gauri8427:
#include<stdio.h>
#include<conio.h>
void main()
{
 printf("%u  \n","hello");
 printf("%u  \n","hello");
}

Output:-
4210688
4210688


How is the two identical strings -> "hello" have same memory address, this shouldn't be the case, they must be stored on different memory addresses.

Miguel Gimenez:

* 1.- This is not related to Embedded development
* 2.- Code::Blocks is an IDE, your question is compiler-related so it is OT here
* 3.- You are telling printf to interpret the first argument as an unsigned int but you are passing a pointer to char

sodev:
First of all, this question is kind of off-topic here.

Second, no, they dont need to. These are not lvalues but rvalues, they dont need to have an address at all. The compiler can freely optimize them, both are identical constant literals and it looks like the compiler decided to perform string pooling here hence they have the same address.

gauri8427:
"You are telling printf to interpret the first argument as an unsigned int but you are passing a pointer to char"
Sir, here I am trying to print the address pointing to first char of string, that's why used format specifier %u because there's no point of memory adddress being negative.

stahta01:
gauri8427: Please read and follow the rules for this website!
http://forums.codeblocks.org/index.php/topic,9996.0.html

Navigation

[0] Message Index

Go to full version