Thread: Pointer to stucture?

  1. #1
    Registered User
    Join Date
    May 2008

    Pointer to stucture?


    Here is a sample of code I will be refering to:

    #include <stdio.h>
    #include "misc.h"
    int main(void){
    double *x,*y;
    data *data_ptr;
    char file[100];
    printf("Enter data file:");
    //getdata returns pointer to data structure
    data_ptr = getdata(file);
    x = data_ptr[0].x;
    y = data_ptr[0].y;
    printf("Value of X = %lf \nValue of Y = %lf\n",*x,*y);
    return 0;
    and the structure looks like this

    typedef struct {
    	unsigned int larray;
    	double *x;
    	double *y;
    So what I am trying to do is, I assign data_ptr the address on the data structure. I then want to initialize the variables x and y to two of the members of the structure but this is not working. Can you point me in the right direction on where I may have gone wrong.

    See below for getdata function:

    data * getdata(const char file[])
    	int numLines = 0;
    	double x[200],y[200];
      	data *data_ptr;
    	char line[80];
    	FILE *file_data;
    	//open file
    	file_data = fopen(file, "r");
    	if (file_data == NULL)
    		printf("Could not open file");
    	//count number of lines in file
    	while ( fgets(line, 80, file_data) != NULL) numLines++;
    	//allocate memory for structure
    	data_ptr = malloc(sizeof(data) * numLines);
    	//initilize values for structure elements
    	for(int count = 0; count <= numLines; count++){
    		fscanf(file_data, "%lf %lf", &x[count], &y[count]);
    			data_ptr[count].larray = numLines;
                data_ptr[count].x = &x[count];
                data_ptr[count].y = &y[count];
    	printf("Address of data_ptr in misc = %i\n",data_ptr);
    	//return data structure pointer address to main
    	return data_ptr;
    and the file I can referring too will just look like something like this (plain text file)

    1.87100000e+03 4.16000000e+00
    1.87200000e+03 4.69000000e+00
    1.87300000e+03 5.40000000e+00
    1.87400000e+03 5.80000000e+00
    1.87500000e+03 5.60000000e+00
    1.87600000e+03 5.55000000e+00
    1.87700000e+03 4.17000000e+00
    1.87800000e+03 4.40000000e+00
    1.87900000e+03 4.05000000e+00

    If you need any more information just ask.

    Thank you

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    And "not working" means what? Edit: If you mean "cannot convert double to pointer", you probably want to change the value pointed to by x, which would be *x = <stuff> and *y = <stuff>.
    Last edited by tabstop; 09-16-2008 at 09:32 AM.

  3. #3
    Registered User
    Join Date
    May 2008
    Yes, that is my problem its not assigning a value to x or y when I try this

    x = data_ptr[0].x;
    y = data_ptr[0].y;

    but I know the value of data_ptr[0].x; is an address in memory, I have confirmed this by doing a printf and looking at the value but for some reason I cant assign it to x or y. Any idea's?

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    How do you know it is not assigning a value to x, y?

    The code you provided compiles fine (excluding the printinfo() function, that is not presented). It gives only a warning with -Wall on gcc, which has to do with this:
    printf("Address of data_ptr in misc = %i\n", data_ptr);
    you need %p not %i

  5. #5
    Registered User
    Join Date
    May 2008
    I know its not assigning a value to x and y because after I try to assign a value I use this code to print the value pointed to by x and y but it just prints 0.00000 0.00000

    printf("Value of X = %lf \nValue of Y = %lf\n",*x,*y);

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Ok, found your errors:

    In getdata():
    -You have a struct that has pointers to doubles. Then you call a function that has x, y at a LOCAL SCOPE. Their values will be lost after the function returns. So the pointers of the struct that you assign to the memory of x, y inside getdata will be pointing at LOCAL SCOPE memory, thus it won't be valid after the function returns.
    Solution: change the pointers to doube x and double y inside struct data. Then assign the value of x, y inside getdata.
    dynamically allocate memory for x, y inside getdata. In contrast with local variables, allocated memory will stay until freed.
    -You open the file to read the lines, and get numlines. The file pointer will be at the end of the file. Then you use fscanf() to read using the same file pointer. You have to set the pointer at the beginning of the file. Do that with fseek() or frewind() (or is it just rewind()?). Or just open and close the file again.
    -Close every file you open (you don't have fclose() inside getdata)

    Now it works fine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM