Thread: Some basic doubts in pointer arithmetic

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    26

    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
    Some basic doubts in pointer arithmetic-1-png

    *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,

    Some basic doubts in pointer arithmetic-2-png

    Now content of a is 101
    Is this how his program works?

    Thanks Athul
    Last edited by Athul; 06-27-2020 at 12:59 PM.

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    951
    Presumably your first example is supposed to call foo1 and your second is supposed to call foo2 (with a properly defined s).

    The postfix increment operator has higher precedence than the dereferencing operator. So foo1 actually says the following and therefore does absolutely nothing. If you are compiling with full warnings you should get a warning.
    Code:
    void foo1(char **p) { *(p++); }  // This is how *p++ is interpreted by the precedence rules
    To make foo1 actually do something (acting like foo2) :
    Code:
    void foo1(char **p) { (*p)++; }
    Or more intelligently, IMO :
    Code:
    void foo1(char **p) { ++*p; }
    Your explanation of why foo2 (and the new-and-improved foo1) do what they do is correct.
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointer doubts...
    By sanddune008 in forum C Programming
    Replies: 11
    Last Post: 07-08-2009, 02:51 AM
  2. Basic floating point arithmetic not working...
    By zodiacWarrior in forum C++ Programming
    Replies: 2
    Last Post: 08-11-2008, 10:16 AM
  3. Pointer arithmetic
    By depietro in forum C Programming
    Replies: 13
    Last Post: 03-29-2007, 06:03 AM
  4. Efficient basic arithmetic algorithms (8 bit)
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-23-2001, 09:56 AM
  5. pointer arithmetic
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-04-2001, 06:45 PM

Tags for this Thread