What is a string?
A string is a sequence of characters terminated with null character '\0'. Consider the following,
"Hello World"
Here, "Hello World" is a string of 11 characters. Size of character is 1 byte. So, we need 12 byte of memory to store the above string. One additional byte is to terminate the string with null character.
+----------------------------------+
|H|E|L|L|O| |W|O|R|L|D|\0| => 11 characters + 1 null character = 12 characters
+----------------------------------+
A string is a sequence of characters terminated with null character '\0'. Consider the following,
"Hello World"
Here, "Hello World" is a string of 11 characters. Size of character is 1 byte. So, we need 12 byte of memory to store the above string. One additional byte is to terminate the string with null character.
+----------------------------------+
|H|E|L|L|O| |W|O|R|L|D|\0| => 11 characters + 1 null character = 12 characters
+----------------------------------+
What is character pointer?
If a pointer points to a memory location of a character, then it is called character pointer. Below is the declaration for character pointer.
char *ptr, ch = 'a';
ptr = &ch;
Here, ptr is a character pointer and it points to the address of the variable ch.
Consider the following,
char *ptr;
ptr = "hello world";
Here, ptr is a pointer to the character array. And it points to the first character of the string constant "hello world".
Difference between character array and pointer to string constant?
char array[] = "hello world";
Above is the declaration for array of character. It allocates memory to hold 12 characters(11 character sequence + 1 null character). And we are allowed to modify value(character) at any index of the array.
char *ptr = "hello world";
Above is the declaration for pointer to a string constant. Pointer ptr points to the address of the first character of the given string constant. Any attempt to modify string constant contents would result to segmentation fault.
Pointer to an array of characters:
char array[] = "hello";
char *ptr;
ptr = array;
Here, ptr is a pointer to an array of characters. Let us see how to access values referred pointers using subscript.
ptr[0] is 'h'
ptr[1] is 'e'
ptr[2] is 'l'
ptr[3] is 'l'
ptr[4] is 'o'
The above statement(ptr = array) doesn't copy the contents of array to ptr. Instead, it copies the reference of array to pointer ptr.
Let us see how to copy contents of one string to another using pointers.
char arr[] = "hello";
char *ptr = (char *)malloc(10 * sizeof(char));
Now, we are going to copy the contents to array arr to the buffer pointed by pointer ptr
while (*arr != '\0') {
*ptr++ = *arr++;
}
*ptr = '\0';
What is the size of character pointer?
Size of the character pointer is 4 bytes.
Why do we need character pointer?
Dynamic memory allocation can be performed using character pointer. Consider the following example, user gives input string as command line argument. We need to copy the input string to another buffer(destination string) and print the contents of the buffer. In this case, we cannot predict the length of the input string. So, static allocation for destination buffer is not possible. Then, the only possibility is dynamic memory allocation. User has to find the length of the string, allocate memory for destination buffer at run time and copy the contents of the input string.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
char *dest, *tmp, *src = NULL;
int len = 0;
src = argv[1];
if (!src) {
printf("Please provide command line input\n");
return 0;
}
// find the length of the input string
while (src[len] != '\0') {
len++;
}
printf("Length of input string: %d\n", len);
/*
* allocate memory for destination buffer -
* one additional byte is for null character
*/
dest = (char *)malloc(sizeof(char) * (len + 1));
tmp = dest;
/*
* copy the contents of input string
* to destination buffer
*/
while ((*tmp++ = *src++) != '\0');
// print the ouput
printf("Output: %s\n", dest);
return 0;
}
Output:
jp@jp-VirtualBox:$ ./a.out helloworld
Length of input string: 10
Output: helloworld
Length of input string: 10
Output: helloworld
How to pass character pointer to a function?
It's very simple. User has pass address of the character variable or address of the first element of character array as function argument. Below example shows how to pass character pointer as argument.
#include <stdio.h>
#include <string.h>
/*
* copies the contents of the buffer pointed
* by src to the buffer pointed by dest
*/
void copystring(char *dest, char *src) {
while ((*dest++ = *src++) != '\0');
return;
}
int main() {
char dest[32], src[] = "helloworld";
/*
* passing address of first element of
* character arrays as arguments
*/
copystring(dest, src);
// printing the output
printf("Ouput: %s\n", dest);
return 0;
}
Output:
jp@jp-VirtualBox:~/$ ./a.out
Ouput: helloworld
Ouput: helloworld
Dell Laptop Repair Center in Noida is no.1 service center which provides door to door services in or its nearby areas. We have expert, technicians who can repair your laptop at your home. . Call us: 9891868324
ReplyDelete