0% found this document useful (0 votes)
14 views8 pages

String and Pointers

The document explains how strings in C act as pointers to their first character, demonstrating this with examples of character arrays and pointer arithmetic. It covers accessing and manipulating string characters using pointers, as well as printing strings with format specifiers. The document also illustrates how to print substrings by adjusting the starting address of the string.

Uploaded by

ajay701390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views8 pages

String and Pointers

The document explains how strings in C act as pointers to their first character, demonstrating this with examples of character arrays and pointer arithmetic. It covers accessing and manipulating string characters using pointers, as well as printing strings with format specifiers. The document also illustrates how to print substrings by adjusting the starting address of the string.

Uploaded by

ajay701390
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

String and Pointer

The string itself act as a pointer.


The string name is a pointer to the first character in the string.

String name == &string[0]


As we all know, the string is a character array which is terminated by the null '\0' character.

Example

char str[6]=”Hello”;

Where,
str is the string name and it is a pointer to the first character in the string. i.e. &str[0].
Similarly, str+1 holds the address of the second character of the string. i.e. &str[1]

To store "Hello", we need to allocate 6 bytes of memory.


5 byte for "Hello"
1 byte for null '\0' character.
Let's print each character address of the string 'str'

%p format specifier is used to printing the pointer address.

Str[i] == *(str+i)
str[i] will give the character stored at the string index i.
str[i] is a shortend version of *(str+i).
*(str+i) - value stored at the memory address str+i. (base address + index)
Let's print each character of a string using *(str+i)

Output:
Accessing string using pointer
Using char* (character pointer), we can access the string.
Example
Declare a char pointer
Assign the string base address(starting address) to the char pointer.

char str[6] = "Hello";


char *ptr;

//string name itself base address of the string


ptr = str; //ptr references str
Where,
ptr - is a character pointer which points the first character of the string. i.e. &str[0]
Like normal pointer arithmetic, if we move the ptr by 1 (ptr+1) position it will point the next
character.

Printing each character address of a string using pointer variable

Output:
Printing each character of a string using the pointer
To print the value stored at each char address, we have to dereference the memory address.
Like below,
*(ptr+i)

Example:

Output:
Manipulating characters of a string using the pointer
Let's change the 4th(index 3) character 'l' as 'o'.
The new string will be "Heloo".
Example:

Output:
%S and string
We can print the string using %s format specifier in printf function.
It will print the string from the given starting address to the null '\0' character.

String name itself the starting address of the string.


So, if we give string name it will print the entire string.

Printing string using %s format specifier

What will be the output if we give str+1?


If we give str+1, the string address moved by 1 byte (which is the address of the second
character)
So, it will print the string from the second character.
Similarly, if we give str+2, it will print the string from the third character.

Lets print the following pattern:


Hello
ello
llo
lo
o
Example:

Output:

You might also like