Sunday 20 October 2013

Strings concepts in C

We can store a string ("sachin", "ram", etc.) using a character array and using a character pointer in C.

Using character array:

A character array is declared as below
char var[] = "sachin";
char var[7] = {'s', 'a', 'c', 'h', 'i', 'n', '\0'}; /* '\0' is string terminator */
/*One extra for string terminator*/
char var[7] = "sachin";

If this string variable is declared in a function then the variable data is stored in stack segment. So, it is possible to modify the content of the string.
var[1] = 'A';

If the string is a global or static variable then it is stored in data segment.
So, the content of the variable can also be modified like,

var[2] = 'C';
var[3] = 'H';

Using character pointer:

A character pointer is declared in two ways.

case-1:

char *var = "sachin";

Here, the string "sachin" is directly assigned to character pointer and it is stored in Read-only memory of of data segment.This string can be shared among functions unlike character arrays. Since this memory is read-only memory, it is not possible to modify the content of the string.

case-2:

char *var = (char *)malloc(sizeof(char) * 7);
var = "sachin";

Here, the string "sachin" is assigned to character pointer which memory is dynamically allocated using malloc(). So, the string memory is allocated in Heap segment.

This string can be shared among functions unlike character arrays. It is stored in Read-write memory (Heap segment), so it is possible to modify the content of the string.

  *(var+0) = 'S';
  *(var+1) = 'a;
  *(var+2) = 'c';
  *(var+3) = 'h';
  *(var+4) = 'i;
  *(var+5) = 'n';  
  *(var+6) = '\0';
  *(var+1) = 'A';  /* No problem: String is now SAchin */

But, if you assign string "sachin" directly to var character pointer and if we want to edit the string then it is not possible. We will see error.

Example:
char *var = (char *)malloc(sizeof(char) * 7);
var = "Sachin";
 *(var+1) = 'A';  /* problem seen */

Using malloc(), a memory of 7 Bytes is allocated in Heap segment and that address is passed to var.

Now, if we assign any string (of size less than or equal to 7) to this var variable then var points to the address of this string which is allocated in Read-Only memory. Because, compiler allocates memory for string "Sachin" in Read-only memory and this address is copied into var character pointer.
So, it is not possible to modify the string of this memory. It falls under case-1 scenario.

Note-1: Benefit from character pointer is that we can share the address of the string among functions.

Sample code for Note-1:
------------------------------

#include<stdio.h>

char* character_pointer_fun()
{
  char *str = "Using character pointer";
 
  return str;
}

char* character_array_fun()
{
  char str[] = "Using character array";
 
  str[1] = 'S';

  return str;
}

int local_var()
{
  int a =111;
  return a;
}

void main()
{
  char *char_ptr_str;
  char *char_array_str;
  int a;
 
  char_ptr_str = character_pointer_fun();
  printf("In main: %s\n", char_ptr_str);
 
  char_array_str = character_array_fun();
  printf("In main: %s\n", char_array_str);
 
  a = local_var();
  printf("In main: local var: %d\n", a);
}

Output:
----------
In main: Using character pointer
In main: USing character array /* Not expected output */
In main: local var: 111 /* Not expected output */

The last two lines output are not expected. Since, after function is returned the stack memory will be cleared for that function.
So, we may or may not see the correct value for local variable and a string which is defined as character array.

No comments:

Post a Comment

You might also like

Related Posts Plugin for WordPress, Blogger...