Playing with array's structures HELP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    Playing with array's structures HELP

    am modifying the two arrays in the first function but when I call them for usage in the second seems like they've not beenn modified at all
    plz help

    Code:
    void MonstreFill(Monstre *Squelettique, Monstre *Grassouillet)
    void test(Monstre *Squelettique, Monstre *Grassouillet)

    Code:
    void MonstreFill(Monstre *Squelettique, Monstre *Grassouillet)
    {
    	int i, nbrMonstre, monstreType, monstreTaille;
    
    	do
    	{
    		puts("Nombre de monstre ?");
    		scanf("%d", &nbrMonstre);
    	}while(nbrMonstre < 1 || nbrMonstre > 10000);
    
    	//initialisation
    
    	for(i = 0; i < nbrMonstre; i++)
    	{
    		Squelettique[i].type = 0;
    		Squelettique[i].taille = 0;
    
    		Grassouillet[i].type = 0;
    		Grassouillet[i].taille = 0;
    	}
    
    	for(i = 0; i < nbrMonstre; i++)
    	{
    		puts("0->grand monstre, 1->petit monstre? largeur(petit monstre), longueur(grand monstre)?");
    		scanf("%d %d", &monstreType, &monstreTaille);
    		if(monstreType == 0)
    			Squelettique[i].taille = monstreTaille;
    		else if(monstreType == 1)
    			Grassouillet[i].taille = monstreTaille; 
    	}
    }


    Code:
    void test(Monstre *Squelettique, Monstre *Grassouillet)
    {
    	int i;
    	for(i = 0; i < 4; i++)
    	{
    		printf("monstre %d longuere %d\n", &Squelettique[i].type, &Squelettique[i].taille);
    	} 
    
    }
    Thx in advance
  • unauthorized
    New Member
    • May 2009
    • 81

    #2
    The 1st function works fine, if not very safe.

    This is not how you use printf. Drop the &, or you will be displaying the variable address, rather than value.

    printf("monstre %d longuere %d\n", Squelettique[i].type, Squelettique[i].taille);

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      I agree with the above user, your two routines look reasonable.

      I would take a look at the pointer values being passed to both of the routines and make sure they are the same. The way your code is set up, the Monstre arrays must be allocated before you call the subroutines.

      Take a look at how the arrays are allocated and passed to these routines.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        What was the operator input during the second 'for' loop in MonstreFill? Notice that if the entered monstreType is neither 0 nor 1 then nothing happens.

        When precisely do mean when you say "when I call them for usage in the second seems like they've not been modified at all". Do you mean that ->type and ->taille are always zero?

        Notice that MonstreFill does not return the entered value of nbrMonstre. That means that nobody else (the caller or the test function) know how many elements in the Squelettique and Grassouillet arrays were initialized.

        I can only assume that you have a static or dynamic allocation of two 10,000-element arrays passed as Squelettique and Grassouillet to MonstreFill. Anything else and there is a strong possibility of memory corruption.

        Comment

        Working...