Strings And Pointers

 

In the C programming language, pointers are a powerful tool that can be used with strings to store and manipulate character arrays more efficiently. This article will provide an in-depth overview of how to use pointers with strings, along with real-world examples to illustrate the concepts. By understanding the basics of pointers and strings, programmers can unlock the full potential of their code.


What are Strings in C?

Strings in C are character arrays terminated by a null character (‘\0’). A string is essentially a sequence of characters stored in an array. For example:

char name[] = “John Doe”;

In this example, a string called name is created and initialized with the value “John Doe”. The string is automatically terminated with a null character by the compiler.


What are Pointers in C?

A pointer is a variable that stores the address of another variable. Pointers can be used to indirectly access and manipulate the value stored in the memory location they point to. For example:

int num = 42;
int *ptr = #

In this example, an integer pointer called ptr is created and initialized with the address of the num variable. The & operator is used to retrieve the address of a variable.


Using Pointers with Strings

Pointers can be used with strings in a similar way as with other variable types. A pointer to a string can be declared and initialized like this:

char name[] = “John Doe”;
char *ptr_name = name;

In this example, a pointer called ptr_name is created and initialized with the address of the first character in the name string. This allows us to access and manipulate the string using the pointer.


Accessing Strings via Pointers

To access the characters of a string using a pointer, we can use a loop and check for the null character. Here’s an example:

#include <stdio.h>
int main(void) {
char str[6] = "Hello";
char *ptr = str;

while (*ptr != '\0') {
printf("%c", *ptr);
ptr++;
}
return 0;
}

In this example, a while loop is used to iterate through the characters of the str string. The loop continues until it encounters the null character. Inside the loop, the printf function is used to print each character, and the pointer is incremented to point to the next character in the string.


Using Pointer to Store String

Instead of using arrays, we can use character pointers to store a string value. For example:

char *strPtr = “Hello”;

In this example, we have created a character pointer strPtr and assigned the string value “Hello” to it. We can represent the character pointer variable strPtr as follows:

Here, the pointer variable strPtr is allocated memory address 8000 and it holds the address of the string value “Hello” i.e., 5000.

To access and print the characters of the string, we can use a loop and check for the null character:

char *strPtr = "Hello";
char *t = strPtr; // temporary pointer variable
while (*t != '\0') {
printf("%c", *t);
t++;
}

 


Modifying Strings via Pointers

Pointers can also be used to modify the characters in a string. For example:

#include <stdio.h>
int main(void) {
char str[] = "Hello";
char *ptr = str;
// Change the first character in the string
*ptr = 'h';
printf("Modified string: %s\n", str);
return 0;
}

 

In this example, the first character of the str string is changed from ‘H’ to ‘h’ by modifying the value stored at the memory location pointed to by the ptr pointer.


Array of Strings

In C, we can create an array of strings using a two-dimensional array. For example:

char cities[4][12] = {“Chennai”, “Kolkata”, “Mumbai”, “New Delhi”};

In this example, a 2D array called cities is created to store the names of four cities. However, this approach can waste memory space, as we allocate a fixed amount of memory for each string.

An alternative and more efficient way to store an array of strings is by using an array of character pointers:

char *city_ptrs[4] = {“Chennai”, “Kolkata”, “Mumbai”, “New Delhi”};

In this example, an array of character pointers called city_ptrs is created to store the names of the four cities.


Accessing Values Pointed by Array of Pointers

To access and print the values pointed by the array of pointers, we can use a loop:

#include <stdio.h>
int main(void) {
char *city_ptrs[4] = {"Chennai", "Kolkata", "Mumbai", "New Delhi"};
int r, c;
for (r = 0; r < 4; r++) {
c = 0;
while (*(city_ptrs[r] + c) != '\0') {
printf("%c", *(city_ptrs[r] + c));
c++;
}
printf("\n");
}
return 0;
}

 

In this example, a nested loop is used to access each character in the array of pointers and print the city names.


Pointers to Arrays of Characters

A pointer to an array of characters can also be declared and initialized like this:

char a[] = “Hello world”;
char (*ptr)[12] = &a;

In this example, a pointer called ptr is created and initialized with the address of an array. This pointer can be used to access and modify the characters in the array.


Advantages of Using Pointers with Strings

• Unlike a two-dimensional array of characters (array of strings), in an array of pointers to strings, there is no fixed memory size for storage.
• The strings occupy as many bytes as required; hence, there is no wastage of space.


C Program Examples for Pointers and Strings

Let’s take a look at some examples to understand the concepts discussed in this tutorial.

Example 1: Printing Array of Strings
#include <stdio.h>
int main(void) {
char *a[5] = {"one", "two", "three", "four", "five"}; // declaring array of pointers to strings at compile time
int i;
printf("The strings are: ");
for (i = 0; i < 5; i++) {
printf("%s ", a[i]); // printing array of strings
}
return 0;
}

 

Output:

The strings are: one two three four five

Example 2: Printing Student Names
#include <stdio.h>
#include <string.h>
int main() {
// initializing the pointer string array
char *students[] = {"bhanu", "ramu", "hari", "pinky"};
int i;
printf("The names of students are:\n");
for (i = 0; i < 4; i++) {
printf("%s\n", students[i]);
}
return 0;
}

Output:

The names of students are:
bhanu
ramu
hari
pinky


Conclusion

In this comprehensive tutorial, we have covered various aspects of using pointers with strings in C programming language. We discussed:
• The concept of strings and pointers in C.
• Creating and accessing strings using pointers.
• Storing strings using pointers instead of arrays.
• Working with arrays of strings and arrays of pointers to strings.
• The advantages of using pointers with strings to save memory and improve efficiency.
• Demonstrated examples to help you understand the concepts effectively.
By understanding and applying these concepts, you can effectively work with strings using pointers in C, optimize memory usage, and write more efficient code. Remember that pointers are powerful tools in C programming, and when used correctly, they can significantly enhance your code’s performance and readability.
**This is the overall concept of String with Pointers, we will discuss the function with pointers in a separate article in detail.**

You May Also Like…

Single Linked List in C

Single Linked List in C

  Single Linked List is a fundamental data structure in the C programming language. It allows us to store and...

0 Comments