2 Attachment(s)
Some basic doubts in pointer arithmetic
Hello,
I'm learning one embedded system course online, and while watching one of those video, I had some doubts about pointer arithmetic,
while I was searching for answers I came across this thread
c - Increment char pointer - Stack Overflow
Code:
#include <stdio.h>
void foo1(char **p) { *p++; }
void foo2(char **p) { *p += 1; }
int main()
{
char *s = "abcd";
char *a = s;
foo2(&a);
printf("%s", a); //abcd
}
OUTPUT : abcd
Q1. Why the parameter of foo1() is double pointer?
I understand how the program works. character pointers are stored in memory like this
Attachment 16103
*p++ ==> *(p++)
p = &a = 500
Derefenceing p will give 100 (address of first character), then increment the pointer, now p is 501
But I don't understand this
Code:
#include <stdio.h>
void foo1(char **p) { *p++; }
void foo2(char **p) { *p += 1; }
int main()
{
char *a = s;
foo2(&a);
printf("%s", a); //bcd
}
how output becomes bcd??
Is it this way
p = 500 (address of a)
*p += 1
*p = *p+1 = *500 + 1 i.e., dereferencing pointer p will give 100(address of first character)
=> *p = 100 + 1
=?*p = 101 ==> p is 500, it's content now changed form 100 to 101,
Attachment 16104
Now content of a is 101
Is this how his program works?
Thanks Athul