Code Snippets C/C++ Code Snippets

C program to demonstrate example of pointer arithmetic.

By: IncludeHelp, On 11 OCT 2016

In this program we will learn how pointer arithmetic works?

In this example there is a character pointer text which is assign a string value "Hello world", there is another character pointer text1 which is assigned with text+6.

Hence, text1 will point the "world" because of text+6.

Let suppose starting address of text is 1000 (which will point "Hello world" from 1000), so after adding 6 to text 1 starting address of text1 will be 1006 (which will point "world").

C program (Code Snippet) - Example of Pointer Arithmetic

Let’s consider the following example:

/*C program to demonstrate example of 
pointer arithmetic*/

#include <stdio.h>

int main(){
	char *text ="Hello world";
	char *text1=text+6;
	
	printf("%s\n",text);
	printf("%s\n",text1);
	
	return 0;
}

Output

    Hello world
    world




Copyright © 2024 www.includehelp.com. All rights reserved.