4

In the following (legal) c code, there is no memory explicitly allotted to the pointer p.

AFAIK, i cannot get an int *p to point to a value 5 without explicitly allocating memory.

int main()
{
    char *p;
    p = "Hello";
    return 0;
}
  • How's char* pointers different
  • Where's the memory allocated for "Hello"
Arjun Sreedharan
  • 11,003
  • 2
  • 26
  • 34
  • As far i remember is that string in a static part of the heap. Ifyou manipulate that sting you automatically work with a copy – rekire Mar 07 '14 at 06:14
  • possible duplicate of [char \*str; str="HELLO"; How does that work without allocating any memory for the string?](http://stackoverflow.com/questions/19720856/char-str-str-hello-how-does-that-work-without-allocating-any-memory-for-the) – Ivan Aksamentov - Drop Mar 07 '14 at 06:15

4 Answers4

8

When you put literal strings such as "Hello" in your program, the compiler creates an array of characters in the data area of the program (called the Data Segment).

When you assign:

p="Hello";

the compiler takes the address of the string literal in the data segment and puts it in the pointer variable p.

Notice that string literals are different to numeric literals. The type of a string literal is const char[] - which you can assign to a char* pointer. An integer literal is just type int.

Also note that a numeric literal does not need to be stored in the data segment - in most cases such literals are placed directly in the machine code instructions. Thus there is no address which you could point p towards.

If you tried to do this (as per your comment):

int *p;
*p = 5;

what you are actually saying is that you want to store the number 5 into the location pointed to by p (which would be undefined in this case, since we never set p to anything). You would probably get a segfault.

If you tried to do this:

int *p;
p = 5;

what you would be telling the compiler to do is to convert the value 5 into a pointer to an integer and store that in p, with the result that the pointer p now points at address 5. And you would probably get a warning like this:

t.c:7: warning: assignment makes pointer from integer without a cast

In other words, you are trying to convert an integer to a pointer - probably not what you thought you were trying to do.

harmic
  • 28,606
  • 5
  • 67
  • 91
  • @ArjunSreedharan updated with some more information which hopefully helps you to understand – harmic Mar 07 '14 at 06:37
1

p is pointing to string literal Hello where memory is allocated from read only data section. And the moment when you modify it you get a Undefined Behavior and may result in to segmentation fault.

C standard says,

String literals - An ordinary string literal has type “array of n const char” and static storage duration (3.7)

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46
1

In C, string literal like "Hello" will be exaluated to a char * type value, i.e. its base address, you could use it to initialize a pointer to char.

In the runtime, string literals usually is located in some read-only memory segmentation.

By the way, it looks like you confused pointer variable and the address pointed to by a pointer variable.

  1. By statements like char *p;, you defined a pointer variable, compiler will allocate the necessary memory to store this variable. But in this time, this pointer has not been initialized, so it does not have a determined value, which means it could point to anywhere.

  2. By statements like p = "Hello";, you assigned pointer p a value, now it points to some determined memory address, this memory segmentation could be allocated by compiler, or be allocated by yourself, for example p = malloc(...);.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
0

The jobs was done for you by the compileer. It allocates space in the data segment and stores the string "Hello" and assigns the base address of this array to the pointer that is p1 in your case.

However this array "Hello" will be a const array. You will not be able to modify the data once it created.

If you try to modify the data then you will get unexpected results.

Kranthi Kumar
  • 1,184
  • 3
  • 13
  • 26