0

The code uses a struct ( int x,y) to print message when c->x increase the value by 1, but unlike c->x , ptr1 pointer "forgets" its address.

How can I create a pointer to string array without "forgetting" its address?

#include <stdio.h>
#define msgROW 5
#define msgLEN 16

struct cursxy{
   unsigned int x;
   unsigned int y;
};

void cursor(struct cursxy *c,char (*ptr1)[msgLEN])
{
   c->x++;
   printf("%s \n", *ptr1++);
}

int main()
{
   struct cursxy cursxy = {0,0};       

   char messages[msgROW][msgLEN] =
    { 
        "Set Duty Cycle", 
        "Set Frequency",  
        "Set Hours",        
        "Set Minutes",     
        "Set Current"     
    };  

    char (*ptrMsg)[msgLEN] = messages;

    //c->x = 1 , prints first message
    cursor(&cursxy,ptrMsg);   //ptrMsg point to first message

    //c->x = 2 , prints again the first message
    cursor(&cursxy,ptrMsg);  //ptrMsg Didn't point to second message  <------------

   // and so on
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Luigi
  • 7
  • 4
  • 2
    In `cursor`, `ptr1++` increments the *local copy* of `ptr1`. The change is not visible in `main`, which has its own independent copy of the pointer. Remember, C uses pass by value to pass function arguments, not pass by reference. In order for `cursor` to be able to change it, you would need to pass its address (or, alternatively, have `cursor` return the new value). – Tom Karzes Jun 20 '21 at 22:53
  • Hi Tom , understand but I'm trying right now to pass ptr1 address as with 'cursxy' for example with cursor(&cursxy,&ptrMsg); but I get an "imcompatible pointer type" message, maybe I need an pointer to pointer , I dont know – Luigi Jun 20 '21 at 23:05
  • Yes, you would need to take the address of the pointer when passing it to the function, and the function would need to add an extra level of indirection. – Tom Karzes Jun 21 '21 at 01:28

2 Answers2

1

The difference between the expressions

c->x++

and

*ptr1++

is that the latter modifies a function argument, whereas the former does not.

In C, functions have their own copy of the values of the function arguments that were used to call the function. As a consequence, modifying these arguments inside the function will not modify the original variable.

Therefore, in the function cursor, any changes to the function argument ptr1 will not change the variable ptrMsg in the function main.

The simplest solution to your problem would be to increment ptrMsg inside the function main instead of inside cursor.

However, if you insist on changing ptrMsg from inside cursor, then you will have to pass the variable ptrMsg by reference instead of by value. This means that you will have to instead pass the address of ptrMsg to the function cursor, like this:

cursor(&cursxy,&ptrMsg);

You will also have to change the prototype of the function cursor, so that the second parameter has an additional layer of indirection. Additionally, you will have to add a * dereference operator in order to access the original variable via the pointer. Afterwards, your function will look like this:

void cursor(struct cursxy *c,char (**ptr1)[msgLEN])
{
    c->x++;
    printf("%s \n", *( (*ptr1)++ ) );
}
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0

this is what you're looking for. same as cursxy, use double pointer.

#include <stdio.h>
#define msgROW 5
#define msgLEN 16

struct cursxy{
   unsigned int x;
   unsigned int y;
};

void cursor(struct cursxy *c, char (**ptr1)[msgLEN])
{
   c->x++;
   
   printf("%s \n", *ptr1);
   *ptr1 = ++*ptr1;
}

int main()
{
   struct cursxy cursxy = {0,0};       

   char messages[msgROW][msgLEN] =
    { 
        "Set Duty Cycle", 
        "Set Frequency",  
        "Set Hours",        
        "Set Minutes",     
        "Set Current"     
    };  

    char (*ptrMsg)[msgLEN] = messages;
    char (**ptr)[msgLEN] = &ptrMsg;
    

    //c->x = 1 , prints first message
    cursor(&cursxy, ptr);   //ptrMsg point to first message

    //c->x = 2 , prints second message
    cursor(&cursxy, ptr);  //ptrMsg Didn't point to second message  <------------
    
        cursor(&cursxy, ptr);  //ptrMsg Didn't point to second message  <------------

   // and so on
}
Denis Turgenev
  • 138
  • 1
  • 9
  • 2
    The line `*ptr1 = ++*ptr1;` [will cause undefined behavior](https://stackoverflow.com/q/949433/12149471), just like `i = ++i;` would cause undefined behavior. Did you maybe intend to write `*ptr1 = *ptr1 + 1;` or `++*ptr1;`? – Andreas Wenzel Jun 20 '21 at 23:44