Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Pointers, Functions and XCode messages

Pointers, Functions and XCode messages

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionswiftdata-structuresperformance
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ericgahn
    wrote on last edited by
    #1

    Hi all; I am relearning C after about 20 years. (I had a done a course after which never wrote a line of C code to earn beer money). I would appreciate any sort of help with my current quandary - a function returning a pointer and returning structure variables.

    #include typedef struct
    {
    float Centre[3];
    float Radius;
    } Sphere;

    Sphere *MakeSphere(float *centre, float *radius);
    void PrintSphereDetails(Sphere *s);

    int main(int argc, const char * argv[])
    {

    float c1\[3\] = {10.0,20.0,30.0};
    float r1 = 10.0;
    Sphere \*s1 = MakeSphere(&c1\[0\], &r1);
    
    PrintSphereDetails(s1);
    
    return 0;
    

    }

    Sphere *MakeSphere(float *centre, float *radius)
    {

    Sphere s;
    
    s.Centre\[0\] = \*centre++;
    s.Centre\[1\] = \*centre++;
    s.Centre\[2\] = \*centre;
    s.Radius = \*radius;
    
    return &s;
    

    }

    void PrintSphereDetails(Sphere *s)
    {

    printf("------------------------------------------------\\n");
    printf("Centre: (%6.2f, %6.2f, %6.2f)\\n", s->Centre\[0\], s->Centre\[1\], s->Centre\[2\] );
    printf("Radius: %6.2f\\n\\n", s->Radius);
    

    }

    I am working in XCode and the code works, but I get a warning "Address of stack memory associated with local variable 's' returned." for the code line return &s in function MakeSphere. That is exactly what, I think, I need so what is XCode telling me? Another problem, which XCode solved for me has to do with the way to get the values of the structure variables in function PrintSphereDetails. Initially I had them as s.Centre[0] but XCode flagged these and recommended doing s->Centre[0]. What is the difference? Regards Eric

    The author's comes from a long line of evolved fish.

    J CPalliniC 2 Replies Last reply
    0
    • E ericgahn

      Hi all; I am relearning C after about 20 years. (I had a done a course after which never wrote a line of C code to earn beer money). I would appreciate any sort of help with my current quandary - a function returning a pointer and returning structure variables.

      #include typedef struct
      {
      float Centre[3];
      float Radius;
      } Sphere;

      Sphere *MakeSphere(float *centre, float *radius);
      void PrintSphereDetails(Sphere *s);

      int main(int argc, const char * argv[])
      {

      float c1\[3\] = {10.0,20.0,30.0};
      float r1 = 10.0;
      Sphere \*s1 = MakeSphere(&c1\[0\], &r1);
      
      PrintSphereDetails(s1);
      
      return 0;
      

      }

      Sphere *MakeSphere(float *centre, float *radius)
      {

      Sphere s;
      
      s.Centre\[0\] = \*centre++;
      s.Centre\[1\] = \*centre++;
      s.Centre\[2\] = \*centre;
      s.Radius = \*radius;
      
      return &s;
      

      }

      void PrintSphereDetails(Sphere *s)
      {

      printf("------------------------------------------------\\n");
      printf("Centre: (%6.2f, %6.2f, %6.2f)\\n", s->Centre\[0\], s->Centre\[1\], s->Centre\[2\] );
      printf("Radius: %6.2f\\n\\n", s->Radius);
      

      }

      I am working in XCode and the code works, but I get a warning "Address of stack memory associated with local variable 's' returned." for the code line return &s in function MakeSphere. That is exactly what, I think, I need so what is XCode telling me? Another problem, which XCode solved for me has to do with the way to get the values of the structure variables in function PrintSphereDetails. Initially I had them as s.Centre[0] but XCode flagged these and recommended doing s->Centre[0]. What is the difference? Regards Eric

      The author's comes from a long line of evolved fish.

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      ericgahn wrote:

      Sphere s;

      That line of code is creating the instance on the stack. The stack is only valid within the method and there is no assurance that it will continue to be valid. You need to allocate it on the heap - look at the malloc method and associated other methods. And remember to delete it when you are done with it.

      E 1 Reply Last reply
      0
      • E ericgahn

        Hi all; I am relearning C after about 20 years. (I had a done a course after which never wrote a line of C code to earn beer money). I would appreciate any sort of help with my current quandary - a function returning a pointer and returning structure variables.

        #include typedef struct
        {
        float Centre[3];
        float Radius;
        } Sphere;

        Sphere *MakeSphere(float *centre, float *radius);
        void PrintSphereDetails(Sphere *s);

        int main(int argc, const char * argv[])
        {

        float c1\[3\] = {10.0,20.0,30.0};
        float r1 = 10.0;
        Sphere \*s1 = MakeSphere(&c1\[0\], &r1);
        
        PrintSphereDetails(s1);
        
        return 0;
        

        }

        Sphere *MakeSphere(float *centre, float *radius)
        {

        Sphere s;
        
        s.Centre\[0\] = \*centre++;
        s.Centre\[1\] = \*centre++;
        s.Centre\[2\] = \*centre;
        s.Radius = \*radius;
        
        return &s;
        

        }

        void PrintSphereDetails(Sphere *s)
        {

        printf("------------------------------------------------\\n");
        printf("Centre: (%6.2f, %6.2f, %6.2f)\\n", s->Centre\[0\], s->Centre\[1\], s->Centre\[2\] );
        printf("Radius: %6.2f\\n\\n", s->Radius);
        

        }

        I am working in XCode and the code works, but I get a warning "Address of stack memory associated with local variable 's' returned." for the code line return &s in function MakeSphere. That is exactly what, I think, I need so what is XCode telling me? Another problem, which XCode solved for me has to do with the way to get the values of the structure variables in function PrintSphereDetails. Initially I had them as s.Centre[0] but XCode flagged these and recommended doing s->Centre[0]. What is the difference? Regards Eric

        The author's comes from a long line of evolved fish.

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        As another option, you could change your function this way

        void MakeSphere(float *centre, float *radius, Sphere *);

        and initialize the struct allocated by the caller.

        Veni, vidi, vici.

        In testa che avete, signor di Ceprano?

        E 1 Reply Last reply
        0
        • J jschell

          ericgahn wrote:

          Sphere s;

          That line of code is creating the instance on the stack. The stack is only valid within the method and there is no assurance that it will continue to be valid. You need to allocate it on the heap - look at the malloc method and associated other methods. And remember to delete it when you are done with it.

          E Offline
          E Offline
          ericgahn
          wrote on last edited by
          #4

          Thank you for your reply. Very helpful. I googled malloc() and used this site to further understand suggested soln. Eric

          1 Reply Last reply
          0
          • CPalliniC CPallini

            As another option, you could change your function this way

            void MakeSphere(float *centre, float *radius, Sphere *);

            and initialize the struct allocated by the caller.

            Veni, vidi, vici.

            E Offline
            E Offline
            ericgahn
            wrote on last edited by
            #5

            CPallini wrote:

            void MakeSphere(float *centre, float *radius, Sphere *);

            So if I understand correctly a Sphere 'instance' would be created in Main() and then a pointer to this instance passed to the function?

            CPalliniC 1 Reply Last reply
            0
            • E ericgahn

              CPallini wrote:

              void MakeSphere(float *centre, float *radius, Sphere *);

              So if I understand correctly a Sphere 'instance' would be created in Main() and then a pointer to this instance passed to the function?

              CPalliniC Offline
              CPalliniC Offline
              CPallini
              wrote on last edited by
              #6

              Yes, you got it right.

              Veni, vidi, vici.

              In testa che avete, signor di Ceprano?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups