Returning pointer from method within code

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adama
    New Member
    • Mar 2008
    • 5

    Returning pointer from method within code

    Hi

    I'm sorry if this seems like a bit of a silly question but i'm very new to c programming and I'm having a few real problems doing this.

    The way my code works is that there is a main method which calls a series of methods to obtain data from a socket and validate the data.

    The issue I'm having is when I come to pass back the validated data to main.

    I have a a 57 character field 'message' that I want to return, however I can't seem to do this so I thought I'd try returning '&message' instead.

    I'm getting a warning that 'function returns an address of a local variable' so I'm assuming that this will be no good as the memory address can only be used by the method and can't be passed back.

    I've had a search about and it talks about using structs for this but I don't have any experience of using them at all.

    Does anyone havea recommendation for doing this?

    Could I pass the memory address from main down into the later methods and then write to that memory address within one of the called methods?

    Thanks in advance
    Adam
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Yes, you could use a char** as a parameter to each of these to safely pass the char* address down through the function call tree. To pass it, use &message (assuming message is a char*).

    Comment

    • adama
      New Member
      • Mar 2008
      • 5

      #3
      Thanks,

      So to clarify, if I call my methods with, for example

      int main()
      {
      char message[57];
      newMethod(&mess age);
      .
      .
      .
      }

      to pass the address down, I could then use

      void newMethod(Char *message)
      {
      .
      .
      .
      *message = newMessage
      }

      which could be read in the calling method?

      Comment

      • adama
        New Member
        • Mar 2008
        • 5

        #4
        If that's not right, is anyone ale to rework that example to show me what I'd be doing wrong please?

        Cheers

        Comment

        • Laharl
          Recognized Expert Contributor
          • Sep 2007
          • 849

          #5
          No, newMethod's signature in the definition (it's called correctly) should be
          [CODE=c]
          void newMethod(char **message);
          [/CODE]

          Other than that, this looks right.

          Comment

          • adama
            New Member
            • Mar 2008
            • 5

            #6
            That's excellent, thanks

            Comment

            • adama
              New Member
              • Mar 2008
              • 5

              #7
              Thanks for all of your help so far, however, I can't get this to compile correctly

              I've pasted the exact code in case it's something i've typed that I'm missing.

              It's happy with the population in handleData, however, the compiler has raised a warning on using &newMessage in main.

              Do you have any ideas please?

              THanks


              int main()
              {
              char newMessage[57];
              int server_socket;
              int returnmess;

              ...... opening socket etc......

              returnmess = handleData(serv er_socket,&newM essage);

              }

              int handleData(int socket,char **newMessage)
              {
              char message[57];

              .....validating message....

              *newMessage = message;
              }

              Comment

              • Laharl
                Recognized Expert Contributor
                • Sep 2007
                • 849

                #8
                Use new to allocate that memory, depending on what the compiler is saying, that might fix it. You do have to use delete[] once you're done with it to get rid of it, though.

                What is the compiler actually saying?

                Comment

                Working...