ADVERTISEMENT
ADVERTISEMENT
Code Snippets C/C++ Code Snippets

C program to demonstrate example of pointer arithmetic.

By: IncludeHelp, On 11 OCT 2016

ADVERTISEMENT

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

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.