News:

When registered with our forums, feel free to send a "here I am" post here to differ human beings from SPAM bots.

Main Menu

identical strings are showing same memory address, why?

Started by gauri8427, September 20, 2020, 02:03:06 PM

Previous topic - Next topic

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

C Programmer working to learn more about C++.
On Windows 10 64 bit and Windows 11 64 bit.
--
When in doubt, read the CB WiKi FAQ. [url="http://wiki.codeblocks.org"]http://wiki.codeblocks.org[/url]