"Hello" and "World" should be string in consecutive locations in the literals block. strlen("Hello") is 6. The values that the poster is displaying are 70 and 76 - a difference of 6 cells. (It would appear that your strings are located in different blocks. Interesting....)

The poster may be using 16-bit integers and therefore getting only the lower 16 bits of the address.

I am trying in Turbo C++ v3.0. I agree to that of Booki because I have modified the program like this

#include<stdio.h>
void main()
{
int i="Hello",j,k;
char * a,*b;
clrscr();
a="";
b="Thanks";
j=a;k=b;
printf("i=%d j=%d k=%d",i,j,k);
getch();
}
and output is i=170 j=176 k=177
But still I am not getting why every first string gets always initialised to an integer with a value of 170 and then others follow as sequentially and also why different platforms are behaving so differently to this.

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Ok, but is the offset 170 a mandatory for initializing a string literal. If yes, then what *type* of data segment is this, can't we modify the value there. According to Booki it is a read only memory then why should any string literal be every time get initialized to an offset of 170 relative to the data segment. What may be stored from offset 0 to 169. Moreover why should a offset value be printed rather than an address value. I have changed the int to unsigned int and the '%d' to '%u' but still the same.

>> But still I am not getting why every first string gets always initialised to an integer
>> with a value of 170 and then others follow as sequentially

170 is simply the location of where that first string is stored. 170 just happens to be the first available slot in this particular case. it is not a special number.

int i = "Hello";

is not good practice but what is happening here is that a string literal ("Hello") is used to initialize the integer i. in the context of an expression, a string literal is converted by the compiler to a pointer to/address of/location of the string literal.in memory which in this case happens to be 170. the others follow immediately after because.. well its a good place to put them, side by side. it's compact.

>> and also why different platforms are behaving so differently to this.

the other platforms/compilers are not behaving differently. the same thing is happening. the only difference is that you're generating 16 bit code as mokule pointed out, so the values appear different but the same thing is happening.

the first results i posted seemed scattered because your sample code was surrounded by a whole lot of other code that i was working on... =)..

if i and j are offset values what is k then. Although i and j points to memory address of the string literals "Hello" and "" and k points to address of j and l points to the address of a. How can we account for this. Please clear my misconception. I have little knowledge about this.

K and L hold offsets for the location of J and A, both of which are on the stack. The stack grows down from high to low so the 'bottom' of the stack would be at offset 65535 (the maximum 16 bit value).

Notice that:
- both J and A are very close to the bottom of the stack
- J is closer to the bottom than A.
- J and A are 6 bytes apart. since one pointer is only 2 bytes there is a gap
of 4 bytes in between. just enough room for K and L. the compiler has placed
your variables on the stack in declaration order.

There is a difference between the address of a pointer, and the contents of the pointer.

The contents of i and j are pointers to static strings, which have values of 170 and 176.

k contains the address of the local variable j - iow, the contents of j are stored at memory address 65520. Memory location 65520 contains 176, which is a pointer to the static string "".

similarly, l contains the address of a.

Think of each memory location as a tiny container. This container has two properties - its address, and its contents. The contents can be a numerical value which represents a number (i.e., 6) or a numerical value which represents another memory location.

For example:

the variable i has address of 65522 (let's say) and contents of 170 (address of "Hello")
the variable j has address of 65520 and contents of 176 (same as a, as j was assigned a's value
the variable k has address of 65518 (let's say) and contents of 65520 (which is j's memory address)
the variable l has address of 65516 (let's say) and contents of 65514 (which is a's address)
the variable a has address of 65514 and contents of 176 (address of "")

address 176 contains the first character of the static string ""
address 170 contains the first character of the static string "Hello"
address 171 (likely) contains the second character of "Hello"
etc.

So they are offsets to the stack segment and the location of the string literals are the offset to the data segment. Since stack grows from high to low we get large values of offsets, but for const string literals it starts from the top. Are they the same segment i.e. SS=DS. I have this much to know .

Avik.

0

Featured Post

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Preface
I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…