hi , i'm having a problem with pointers in C ...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blackslither
    New Member
    • Oct 2008
    • 5

    hi , i'm having a problem with pointers in C ...

    I will give a simple example inspired from my problem :

    void change(int *x, int *y){x=y;}

    int main(){
    int *xx,*yy;

    yy = (int*)malloc(si zeof(int));
    change(xx,yy);

    if(xx == NULL)printf("is null\n");
    else printf("---- %d ----\n",*xx);

    ............... ............... ............... ..............

    xx doesn't point to the value of yy (*yy) , the program prints "is null" . Why ?

    xx and yy must be declared int* , it's something that i must do in my program .
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Is there some reason why couldn't replace "change(xx,yy); " with "xx = yy;" ?

    Take a closer look at the change() function. You're trying to copy the value of one pointer argument into another pointer argument.
    Code:
    void change(int *x, int *y) {
       x = y;
    }
    Consider a similar function that seeks to copy the value of one integer argument into another integer argument. All I've done is change the data type of the arguments, not the basic mission of the function.
    Code:
    void changeInt(int x, int y) {
       x = y;
    }
    Why doesn't changeInt() work? It is the same reason that change() doesn't work.

    Comment

    • blackslither
      New Member
      • Oct 2008
      • 5

      #3
      then how can i copy the value of one pointer argument into another pointer argument ?

      xx = yy must be done in a function . the original program is much complex and it has to be done that way .

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Originally posted by blackslither
        then how can i copy the value of one pointer argument into another pointer argument ?
        How would you copy the value of one integer argument into another integer argument?

        Comment

        • blackslither
          New Member
          • Oct 2008
          • 5

          #5
          Originally posted by donbock
          How would you copy the value of one integer argument into another integer argument?

          change(int*x , int* y){*x=*y;}

          main(){
          int x,y;
          y=7;
          change(&x,&y);
          }

          to be similar to the one with pointers , i would do it this way

          Comment

          • blackslither
            New Member
            • Oct 2008
            • 5

            #6
            Originally posted by donbock
            How would you copy the value of one integer argument into another integer argument?

            got it 10x , correct me if i'm wrong .. .....

            void change(int **x, int **y){*x=*y;}

            int main(){
            int *xx,*yy;

            yy = (int*)malloc(si zeof(int));
            change(&xx,&yy) ;

            if(xx == NULL)printf("is null\n");
            else printf("---- %d ----\n",*xx);

            Comment

            • donbock
              Recognized Expert Top Contributor
              • Mar 2008
              • 2427

              #7
              That should work. Did you get the right answer?

              I would recommend the following variation:
              Code:
              void change (int **xx, int *yy) {
                 *xx = yy;
              }
              Or more generally, for any type T:
              Code:
              void change (T *out, T in) {
                 *out = in;
              }
              The difference lies in not using a pointer for the input value. That reduces the chances that through some typo you might inadvertently change the input value. Sometimes you can't avoid using a pointer for the input, such as when the input is a structure.

              Another variation is
              Code:
              int *change(int *yy) {
                 return yy;
              }
              However, I'd advise not using the return value to copy a structure. The C Standard tells you that a function can indeed return a structure; but what it doesn't tell you is that some compilers implement that feature in a way that isn't thread safe. At this point in your C experience you're probably not worried about thread-safety, but it is never too early to learn good habits.

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                Code:
                yy = (int*)malloc(sizeof(int));
                change(&xx,&yy);
                if(xx == NULL)printf("is null\n");
                else printf("---- %d ----\n",*xx);
                Um, you have a different problem here. Now that xx isn't NULL you'll take the "else" branch. What's going to happen when you dereference xx in the printf argument list?

                Comment

                Working...