Jump to content

Structures

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
I searched through the forum and did not find anything in enough detail to help me.
I am using a pointer to a structure like an array. So lets say i have a struct named People. I have: People * person. I am constantly adding to the struct by reallocating space in person for a new person entry. This works well for my purpose. When i set the data fields like person[i].age person[i].name, person[i].height; everything works well. and i print from main as i read just to test. When i try to print the data from another function only my string fields gets messed up. For example age,height,weight, are all person variable declared before and after name in People and print normally. Cant figure out why......:sleep:

void printRec(Rec *rec, int size)

{

    int i;

    for(i = 0; i < size; i++)

    {

        printf("%d ",person[i].age);

        printf("%s ",person[i].fname);   //messed up: prints last char in string

        printf("%s ",person[i].lname);  //messed up: prints a character not in string

        printf("%c ",person[i].gender);

        printf("%d ",person[i].height);

        //etc

        printSeperator();

    }


}

Ideas?
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#2
veda87

veda87

    Programmer

  • Members
  • PipPipPipPip
  • 126 posts
can you post the entire code or just post how you have defined your structure.. Do you dynamically allocate memory for fname and lname or is it static..

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts

veda87 said:

can you post the entire code or just post how you have defined your structure.. Do you dynamically allocate memory for fname and lname or is it static..


typedef struct{      

        int weight,height;

        char *fname,*lname;

        char gender;

        double points;

        }People;

Things in main

char * temp = readString(file pt);

person[index].fname = temp;

//this print only works properly in main

printf(person[index].fname) 

i allocate memory dynamically on each loop and error test..like i said it gets into the struct but somehow only when i leave main..those two fields print weird
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
ok i did some more testing it only prints the correct value while in loop execution


while (continew)

{

               buffer = (char*)malloc(sizeof(char));

                if (testMemoryPointer(buffer))

                {

                    buffer = readString(inputFile);


                    person[count -1].fname = buffer;

                    printf(" fname: %s\n",person[count -1].fname);//prints good

                    free(buffer);

                }

}end loop


//try to print outside of while loop in main...i adjusted the index to suit

//and i get the same problem....so im wondering if i some how overwrote it or //something..



printf(person[index].fname)//prints weird



Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
No worries i found. I free the memory in another function....
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused: