Code::Blocks Forums

User forums => Embedded development => Topic started by: gauri8427 on September 20, 2020, 02:03:06 pm

Title: identical strings are showing same memory address, why?
Post by: gauri8427 on September 20, 2020, 02:03:06 pm
#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.
Title: Re: identical strings are showing same memory address, why?
Post by: Miguel Gimenez on September 20, 2020, 02:54:53 pm
Title: Re: identical strings are showing same memory address, why?
Post by: sodev on September 20, 2020, 02:57:14 pm
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.
Title: Re: identical strings are showing same memory address, why?
Post by: gauri8427 on September 21, 2020, 10:30:06 am
"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.
Title: Re: identical strings are showing same memory address, why?
Post by: stahta01 on September 21, 2020, 10:35:26 am
gauri8427: Please read and follow the rules for this website!
http://forums.codeblocks.org/index.php/topic,9996.0.html (http://forums.codeblocks.org/index.php/topic,9996.0.html)