0

I am new to C. For storing the string literals, I saw two ways which is showed below

    char s[]="Mohan";
    char *ptr="Mohan";

So, what is the difference between these two. And how memory is allocated for these two statements.

Thanks in advance...

QuestionC
  • 10,006
  • 4
  • 26
  • 44
mohangraj
  • 9,842
  • 19
  • 59
  • 94

4 Answers4

5

In the first case …

char s[] = "Mohan";

… the s variable is an array of characters. However, the second case is something like this:

char s[] = "Mohan";
char *ptr = s;

Actually there is the same array of characters somewhere but in addition to that you have a pointer variable that points to that array.

dlask
  • 8,776
  • 1
  • 26
  • 30
  • They are different. If you need just the string the first choice is for you. If you need a pointer variable that points to "Mohan" now but to "SomethingElse" then, the second choice is there. – dlask Jul 03 '15 at 06:03
  • So, Both the declarations are same or different. My question is If I declared the array, the memory is allocated in the stack and the string is stored in that array. But, If I use the character pointer, Where the string is stored. *ptr having the ability to store only the address. So, *ptr having the address of the first element of the string, How, it correctly print upto end of the string. Even though we are not declared the '\0' character at the end of the string. – mohangraj Jul 03 '15 at 06:07
  • The string "Mohan" is always present in the data section of the program, and automatically terminated with `'\0'`. The first choice creates its copy on the stack while the second choice creates only a pointer variable on the stack that points to the data section. – dlask Jul 03 '15 at 06:10
1

The difference between the two is that char s[]="Mohan"; is stored in the read-write area of the memory layout of the process.

Whereas char *ptr="Mohan"; the string literal Mohan is stored in read-only area.

which means you can point the character pointer to any other string literal but cannot edit the string.

#include <stdio.h>

int main(void)
{
    char a[] = "Stack";  //Here a is an array of characters
    char *p = "overflow"; // p is a pointer to char

    a[0] = 's';
    printf("%s\n", a); //Output is "stack"
    // *p = 'O'; This will cause segmentation fault as you are writing to a read
    // only memory 
    p = a;
    *p = 'O';
    printf("%s\n", a); //Output is "Otack"

    return 0;
}
Santosh A
  • 5,173
  • 27
  • 37
  • This is not the main difference. Pointers and arrays are different, even if sometimes arrays are *decayed* into pointers. – Basile Starynkevitch Jul 03 '15 at 04:40
  • @BasileStarynkevitch : Yes I agree that sometimes arrays are decayed into pointers. But in the question OP has initialized the char pointer to a string literal and the question is about the memory allocation. – Santosh A Jul 03 '15 at 04:51
  • So, there is two memory areas which is called as read only area and the read write area. Until I know about the following memory areas which is used at the time of executing the program which is called as text segment, global variable segment, stack and heap. So, at these major memory, where the read only and the read write memory is allocated. – mohangraj Jul 03 '15 at 04:58
  • @mohanraj : Refer this http://www.geeksforgeeks.org/memory-layout-of-c-program/ – Santosh A Jul 03 '15 at 07:35
1
char s[]="MOHAN";

In this string is declared as character array and stored like other types of arrays. String will be stored in read\write area .Here programmer doesn't need to know size of it as it will be determined by the initialized data. This can be also written as

char s[6]="MOHAN";

Remember at last of array '\0' is stored.

char *ptr="MOHAN";

The string values is directly assigned to a pointer and is stored in shared read only location but pointer is stored in read\write memory. So this kind should be used only when we don't have to change or modify string at later stage in program.

ameyCU
  • 16,489
  • 2
  • 26
  • 41
  • As per my learning, I studied about the following are the memory used to execute the program. They are text segment, global segment, stack and heap. Here, in which part, the read only and the read write memory are placed – mohangraj Jul 03 '15 at 06:13
  • read only is placed in text segment so as to avoid accidental modification of program. And read-write is placed in data segment as they may or may not be modified during run time . – ameyCU Jul 03 '15 at 06:56
0

For this,

char s[]="Mohan";

The memory is allocated in the read and write memory area in stack memory.

But in this case,

char *s="Mohan";

The memory is not allocated in read and write area. The memory is allocated in read only memory area in heap memory. The heap memory contains two parts. Read and write memory area and read only memory area.