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. Pointer - output

Pointer - output

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 4 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.
  • D Offline
    D Offline
    daavena
    wrote on last edited by
    #1

    Hi all, I would like to have a pointer(parameter) as a output of function. Is it possible?? here is my examle: void a(char *pcOutput){ pcOutput = new char[7];// here I will get an new address of heap strcpy(pcOutput,"Hello.\0"); } int main(int argc, char* argv[]) { char *pcOut = NULL; a(pcOut); printf("pointer: %s\n",pcOut);// I see - pointer: null return 0; } What am I doing wrong? Thank you.

    M W 2 Replies Last reply
    0
    • D daavena

      Hi all, I would like to have a pointer(parameter) as a output of function. Is it possible?? here is my examle: void a(char *pcOutput){ pcOutput = new char[7];// here I will get an new address of heap strcpy(pcOutput,"Hello.\0"); } int main(int argc, char* argv[]) { char *pcOut = NULL; a(pcOut); printf("pointer: %s\n",pcOut);// I see - pointer: null return 0; } What am I doing wrong? Thank you.

      W Offline
      W Offline
      willy_total
      wrote on last edited by
      #2

      You need to have a pointer to a pointer. void a(char **ppcOutput) You also need to free the memory in the main function.

      D 1 Reply Last reply
      0
      • D daavena

        Hi all, I would like to have a pointer(parameter) as a output of function. Is it possible?? here is my examle: void a(char *pcOutput){ pcOutput = new char[7];// here I will get an new address of heap strcpy(pcOutput,"Hello.\0"); } int main(int argc, char* argv[]) { char *pcOut = NULL; a(pcOut); printf("pointer: %s\n",pcOut);// I see - pointer: null return 0; } What am I doing wrong? Thank you.

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        You are only passing your pointer by value.  In order for a called function to alter the passed value, it needs to be passed by reference...

        void a(char ***&**pcOutput){

        pcOutput = new char[7];// here I will get an new address of heap
        strcpy(pcOutput,"Hello.\0");

        }

        int main(int argc, char* argv[])
        {
        char *pcOut = NULL;
        a(pcOut);
        printf("pointer: %s\n",pcOut);
        delete[] pcOut;
        return 0;
        }

        or you could return a pointer...

        char *a(){

        char *pcOutput = new char[7];// here I will get an new address of heap
        strcpy(pcOutput,"Hello.\0");
        return pcOutput;
        }

        int main(int argc, char* argv[])
        {
        char *pcOut = a();
        printf("pointer: %s\n",pcOut);
        delete[] pcOut;
        return 0;
        }

        Mark

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        CPalliniC D 2 Replies Last reply
        0
        • M Mark Salsbery

          You are only passing your pointer by value.  In order for a called function to alter the passed value, it needs to be passed by reference...

          void a(char ***&**pcOutput){

          pcOutput = new char[7];// here I will get an new address of heap
          strcpy(pcOutput,"Hello.\0");

          }

          int main(int argc, char* argv[])
          {
          char *pcOut = NULL;
          a(pcOut);
          printf("pointer: %s\n",pcOut);
          delete[] pcOut;
          return 0;
          }

          or you could return a pointer...

          char *a(){

          char *pcOutput = new char[7];// here I will get an new address of heap
          strcpy(pcOutput,"Hello.\0");
          return pcOutput;
          }

          int main(int argc, char* argv[])
          {
          char *pcOut = a();
          printf("pointer: %s\n",pcOut);
          delete[] pcOut;
          return 0;
          }

          Mark

          Mark Salsbery Microsoft MVP - Visual C++ :java:

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

          A reference to a char pointer. Noooooooooooooooooooooooooooooooooooooooooo! (Sorry only personal taste :rolleyes:) :-D

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

          In testa che avete, signor di Ceprano?

          M 1 Reply Last reply
          0
          • CPalliniC CPallini

            A reference to a char pointer. Noooooooooooooooooooooooooooooooooooooooooo! (Sorry only personal taste :rolleyes:) :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            hehe yeah, but it's perfectly valid - It's just a reference to a pointer, and a pointer is just another data type. Maybe "char* &" is better style... ;P

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            1 Reply Last reply
            0
            • M Mark Salsbery

              You are only passing your pointer by value.  In order for a called function to alter the passed value, it needs to be passed by reference...

              void a(char ***&**pcOutput){

              pcOutput = new char[7];// here I will get an new address of heap
              strcpy(pcOutput,"Hello.\0");

              }

              int main(int argc, char* argv[])
              {
              char *pcOut = NULL;
              a(pcOut);
              printf("pointer: %s\n",pcOut);
              delete[] pcOut;
              return 0;
              }

              or you could return a pointer...

              char *a(){

              char *pcOutput = new char[7];// here I will get an new address of heap
              strcpy(pcOutput,"Hello.\0");
              return pcOutput;
              }

              int main(int argc, char* argv[])
              {
              char *pcOut = a();
              printf("pointer: %s\n",pcOut);
              delete[] pcOut;
              return 0;
              }

              Mark

              Mark Salsbery Microsoft MVP - Visual C++ :java:

              D Offline
              D Offline
              daavena
              wrote on last edited by
              #6

              char *&pcOutput I have never seen it before. ;) Thank you for your help.

              1 Reply Last reply
              0
              • W willy_total

                You need to have a pointer to a pointer. void a(char **ppcOutput) You also need to free the memory in the main function.

                D Offline
                D Offline
                daavena
                wrote on last edited by
                #7

                I will try it. Thank you for your help.

                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