Sunday, 21 June 2015

Accessing arrays with pointers | Strings in Programming

We can access array using *(ptr+i)

So, let us see what this means

(ptr+i) for i=0, means ptr i.e. *(ptr+i) for i=0 is *ptr
When we add i, it means adding i number of locations to ptr and finding its value at ptr+i.

sizeof(x) operator tells the size of that variable x.

Arithmetic with Pointers

Increment/ Decrements
Comparing pointers
Subtraction with pointers

String

Sequence of character is referred to as string. The last character of string is '\0' null character.

char s1[]="H"; //constant pointer to string
char s2='H'; //
char *s3="H"; //pointer to constant string

sizeof(s1); //gives 2
sizeof(s2); //gives 1
sizeof(s3); //gives 2

Notion of the string is not a basic datatype in C. String is an array of character i.e. it is an aggregate data type of characters.

Functions of String:
char* strcmp(char *str1,char *str2)




No comments:

Post a Comment