712 views
1 votes
1 votes

How to perform string and char operation in programming ? Please explain and also provide the solution

3 Answers

2 votes
2 votes
go points to "yolo"

1- go+2 will point to l, "%s" will print= lo;

2- ++go will make go=go+1;  go will now point to o- "%s" prints olo;

2- According to Dennis Ritchie's book ( who created c programming language) - an array of characters and pointer to string constant both are different,

char g[]="now is the time"; //an array of characters

char *g="now is the time"; //a pointer to sting constant

individual characters within an array can be changed  but g will always going to point to same storage. On the other hand, the pointer g may subsequently be modified to point somewhere else, but the result is undefined if you try to modify the string contents.

now coming back to question

++go[1] = ++(go[1])  // [] has higher priority than ++ operator

go[1] points to l ; // note- go was incremented previously

but ++(go[1]) is changing string contents hence segmentation fault is there.
0 votes
0 votes
Here, go is not an array but string literals.So, we cannot access their element like an array. Here, go array does not exist but you try to access them so it gives segmentation fault.

Related questions

642
views
1 answers
0 votes
Magma asked Dec 26, 2018
642 views
Please someone explain me properly
312
views
1 answers
2 votes
Akriti sood asked Jan 23, 2017
312 views
will there be no compile time error as we are initialising array greater than its size??
687
views
1 answers
8 votes
GO Classes asked Feb 5
687 views
Consider the following C code:double A[2][3] = {{1, 2, 3}, {4, 5, 6}};Assume that A[0] = 0xFFAA0000 and the sizeof(double) is 8.What will be the value of `A[1]`?0xFFAA00240xFFAA00030xFFAA000C0xFFAA0018