How to pass a pointer from a function to an other??

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #16
    Code:
    void main()
    {
    ...
    }
    int main()

    Always, every time. void is a extension supported by a few compilers but on any other system it is undefined behaviour.

    Code:
    void MonstreFill(Monstre *Squelettique, Monstre *Grassouillet, int *nbrSquelettique, int *nbrGrassouillet)
    {
    	<snip>
    
    		switch (monstreType)
    		{
    		case 0:
    			Squelettique[i].taille = monstreTaille;
    			Squelettique[i].type = 0;
    			j--;
    			break;
    		case 1:
    			Grassouillet[j].taille = monstreTaille;
    			Grassouillet[j].type = 1;
    			nbrGrassouillet++;
    			i--;
    			break;
    		default: 
    			puts("If you are reading this you don't existe !!!");
    		}
    	}
    	nbrSquelettique = i;
    	nbrGrassouillet = j;
    }
    Everywhere you access nbrSquelettique and nbrGrassouillet you write the local pointer value, you don't use the pointer to access the external variable in the calling function so when it is passed to the next function no data has been changed. You want the syntax

    (*nbrGrassouill et)++;
    or
    *nbrSquelettiqu e = i;

    Code:
    void test(Monstre *Squelettique, Monstre *Grassouillet, int *nbrSquelettique, int *nbrGrassouillet)
    {
    /* Same error in this function too */
    }

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #17
      it was the first thing Iu wrote but gave me a run time error!!!
      don't know y???

      Comment

      • momotaro
        Contributor
        • Sep 2006
        • 357

        #18
        this is the error:

        Unhandled exception at 0x00f219a5 in Choix des Armes.exe: 0xC0000005: Access violation reading location 0x00000000.

        Comment

        • momotaro
          Contributor
          • Sep 2006
          • 357

          #19
          normally this error emergs when you are trying to access a non memory allocated pointer !!!!

          Comment

          • momotaro
            Contributor
            • Sep 2006
            • 357

            #20
            using visual studio 2008 pro

            Comment

            • scruggsy
              New Member
              • Mar 2007
              • 147

              #21
              Originally posted by momotaro
              normally this error emergs when you are trying to access a non memory allocated pointer !!!!
              Yes, exactly.
              In your main routine you did this:
              Code:
                  int *longestMonstre, *largestMonstre, *nbrSquelettique = 0, *nbrGrassouillet = 0;
              creating 4 pointers to int initialized to zero.
              You never assign a valid address to nbrSquelettique or nbrGrssouillet in your code, so *nbrSquelettiqu e is an error - the pointer doesn't point to anything.
              In MonstreFill() you do this:
              Code:
                   nbrSquelettique = i;
                  nbrGrassouillet = j;
              assigning integer values to pointer variables. That's not valid.

              It seems to me that what you want to do is this:
              Code:
              int main()
              {
                int nbrSquelettique, nbrGrssouillet = 0;  // these are int, not int*
                MonstreFill(blah, blah, &nbrSquelettique, &nbrGrssouillet);  // pass the ADDRESSES of your ints
                // ...
              }
              
              void MonstreFill(blah, blah, int* nbr1, int* nbr2)
              {
                // ...do stuff
                *nbr1 = i;  // now nbrSquelettique's value has changed
                *nbr2 = j;  // same for nbrGrassouillet
              }

              Comment

              • momotaro
                Contributor
                • Sep 2006
                • 357

                #22
                thank you that is working!!

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #23
                  See I said things would get fixed more quickly if you provided a proper example and explanation :D

                  Comment

                  Working...