passing in structure pointers and returning structure pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nano2
    New Member
    • Jan 2007
    • 41

    passing in structure pointers and returning structure pointers

    Hi ,

    Have the following case passing in structure pointers and returning structure pointers

    can anyone spot whats wrong with this

    structure name is structCar
    Code:
    void callfn( ){
    
    structCar *Result;                               
    char var1[20], var2[20], var[20];
    
    strcpy (var1,"test1");
    strcpy (var2,"test2");
    *Result = myfunc(var1,var2);
    
    callfn2(&result,var);
    
    
    }
    
    
    
    
    structCar * myfunc(char *abc char *bef){
    
    
    resPtr =  (structCar*) malloc (sizeof(structCar) * 2); 
    
    anotherfncall(&result, &abc, &bef)
    
    resPtr = &result;
    
    return resPtr; /*Returns a ptr to a structure */
    }
    
    
    
    void callfn2(const structCar *, char *){
    
    other stuff
    
    }
    
    
    
    anotherfncall(structCar *, const structCar *, const structCar *){
    
    }
    Last edited by horace1; Jan 30 '07, 06:30 PM. Reason: added code tags
  • arne
    Recognized Expert Contributor
    • Oct 2006
    • 315

    #2
    Originally posted by nano2
    Hi ,

    Have the following case passing in structure pointers and returning structure pointers

    can anyone spot whats wrong with this

    structure name is structCar

    void callfn( ){

    structCar *Result;
    char var1[20], var2[20], var[20];

    strcpy (var1,"test1");
    strcpy (var2,"test2");
    *Result = myfunc(var1,var 2);

    callfn2(&result ,var);


    }




    structCar * myfunc(char *abc char *bef){


    resPtr = (structCar*) malloc (sizeof(structC ar) * 2);

    anotherfncall(& result, &abc, &bef)

    resPtr = &result;

    return resPtr; /*Returns a ptr to a structure */
    }



    void callfn2(const structCar *, char *){

    other stuff

    }



    anotherfncall(s tructCar *, const structCar *, const structCar *){

    }

    In function callfn() you're mixing upper and lower case ('Result' and 'result').
    In function myfunc() the variable 'result' is not known.
    And there are some more syntax errors.
    I guess this is not a copy&paste of your code, is it?

    But the main issue may be that myfunc() returns a (structCar *), which you try to assign to *Result, where you should use Result only, since it is of type (structCar *).

    And the call to callfn2 should take Result and not &Result as its argument ... what did your compiler say?

    Comment

    • horace1
      Recognized Expert Top Contributor
      • Nov 2006
      • 1510

      #3
      it is difficult to comment as there are so many basic syntax errors in the code (missing , ; and undefined variables, etc)
      however, a couple of things (and there are more)
      Code:
      structCar * myfunc(char *abc char *bef){
      resPtr = (structCar*) malloc (sizeof(structCar) * 2);
      anotherfncall(&result, &abc, &bef)
      resPtr = &result;
      return resPtr; /*Returns a ptr to a structure */
      }
      you allocate some memory using malloc() and point resPtr to it and two lines later assign the address of result to resPtr?

      the second and third parameters to anotherfncall() are const structCar *
      Code:
      anotherfncall(structCar *, const structCar *, const structCar *){
      yet you call it
      Code:
      anotherfncall(&result, &abc, &bef)
      where abc and bef are char *

      I think you need to post the actual code

      Comment

      • nano2
        New Member
        • Jan 2007
        • 41

        #4
        Originally posted by horace1
        it is difficult to comment as there are so many basic syntax errors in the code (missing , ; and undefined variables, etc)
        however, a couple of things (and there are more)
        Code:
        structCar * myfunc(char *abc char *bef){
        resPtr = (structCar*) malloc (sizeof(structCar) * 2);
        anotherfncall(&result, &abc, &bef)
        resPtr = &result;
        return resPtr; /*Returns a ptr to a structure */
        }
        you allocate some memory using malloc() and point resPtr to it and two lines later assign the address of result to resPtr?

        the second and third parameters to anotherfncall() are const structCar *
        Code:
        anotherfncall(structCar *, const structCar *, const structCar *){
        yet you call it
        Code:
        anotherfncall(&result, &abc, &bef)
        where abc and bef are char *

        I think you need to post the actual code





        This is what I have but I am getting problems with the compiler with the structure assignment any ideas here


        Code:
        main () {
           structCar *resPtr;
           resPtr = testfn();
           testfn2(resPtr);
        }
        
        structCar *testfn() {
            structCar testfnRet;
            return &testfnRet;
        }
        
        int testfn2(StructCar ab) {
            if (strcmp(testfn3(&ab,string) == 0)
                return 1;
            else 
                return 0;
        }
        
        char * testfn3(const structCar *, char *);
        Last edited by Ganon11; Feb 2 '07, 06:32 PM. Reason: code tags added, indented

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Originally posted by nano2
          This is what I have but I am getting problems with the compiler with the structure assignment any ideas here


          Code:
          main () {
             structCar *resPtr;
             resPtr = testfn();
             testfn2(resPtr);
          }
          
          structCar *testfn() {
              structCar testfnRet;
              return &testfnRet;
          }
          
          int testfn2(StructCar ab) {
              if (strcmp(testfn3(&ab,string) == 0)
                  return 1;
              else 
                  return 0;
          }
          
          char * testfn3(const structCar *, char *);
          I'm fairly certain there's a problem in testfn(). Within the function, you allocate memory for a structCar object, then return the address in memory of the object. But once the function is ended, testfnRet has gone out of scope, and so that place in memory will be de-allocated - thus, your pointer will point to a blank space in memory.

          Comment

          • willakawill
            Top Contributor
            • Oct 2006
            • 1646

            #6
            Hi. This is a better way to return a pointer.

            Code:
            structCar *testfn() {
                structCar *testfnRet = new structCar;
                return testfnRet;
            }
            And, because we have used the 'new' operator:
            Code:
            main () {
               structCar *resPtr;
               resPtr = testfn();
               testfn2(resPtr);
               delete resPtr;
            }

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Is the "new"/"delete" option availible in C, or just C++?

              Comment

              • willakawill
                Top Contributor
                • Oct 2006
                • 1646

                #8
                ooops!

                Code:
                structCar *testfn() {
                    structCar *testfnRet;
                    testfnRet = (structCar*) malloc (sizeof(structCar));
                    return testfnRet;
                }

                Comment

                • nano2
                  New Member
                  • Jan 2007
                  • 41

                  #9
                  Originally posted by willakawill
                  ooops!

                  Code:
                  structCar *testfn() {
                      structCar *testfnRet;
                      testfnRet = (structCar*) malloc (sizeof(structCar));
                      return testfnRet;
                  }

                  Thanks For this it has helped but the call to testfn2() is causing problems can you spot it ?

                  int res = testfn2(resPtr) ;
                  which has the following prototype
                  int testfn2 (StructCar ab)

                  Thanks

                  Comment

                  • Ganon11
                    Recognized Expert Specialist
                    • Oct 2006
                    • 3651

                    #10
                    Code:
                    int testfn2(StructCar ab) {
                        if (strcmp(testfn3(&ab,string) == 0))
                            return 1;
                        else 
                            return 0;
                    }
                    
                    char * testfn3(const structCar *, char *);
                    It appears that there is an error in your if...branch. Is it just me, or did you never pass anything for the second argument of testfn3? It calls for a char*, but you pass it a string - string is not declared as a local variable, and you are not referencing it from any StructCar variable.

                    Also, there was a parentheses missing from the if condition. I have added this.

                    Finally, make sure you have actually defined testfn3 - all we see here is a prototype!

                    Comment

                    Working...