473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Passing pointer to function - C programming

Hey guys,
I have a function which needs to read a file and use the pointer parameter given.
the pointer is a structure with five members; int points, float *time, *dhdt, *drate and *diff.
The .dat file contains double values in three columns. I am required to open the .dat file, read the values and insert them into their appropriate members
(1st column of data into *time, 2nd column into *dhdt and 3rd column into *drate).
I have already dynamically allocated memory into the above members.
(data->time = (float *)malloc(sizeof(float), etc);
The variable "points" inside the structure is a user input which is used as the number of lines to read from the file
Here is my code:
Expand|Select|Wrap|Line Numbers
  1. File contains 1800 lines altogether
  2. 0.00000000               -314.18      -304.77
  3. 1.0000e-04    -423.69    -297.31
  4. 0.00020000    -259.91    -289.69
  5. ...
  6. ...
  7.  
  8. // (d) readData function
  9. void readData(simData *data)
  10. {
  11. //open file coding
  12. FILE *file_data;
  13. int k;
  14.  
  15. if ((file_data = fopen("dissipate.dat", "r"))==NULL) {
  16. printf("Cannot open file.\n");
  17. exit(1);
  18. }
  19. else
  20. {                    
  21. while((fscanf(file_data, "%f %f %f", &data->time[k], &data->dhdt[k], &data->drate[k]))== 3);
  22.  
  23. }
  24. fclose(file_data);
how would you get the value of the 1st column to go into the time array?
I tried using a for loop to count through data->points times and print the values line by line but my program crashes.
Any help is much appreciated
Sep 24 '07 #1
2 1576
Banfa
9,065 Expert Mod 8TB
You haven't shown your memory allocation which is the most likely cause of the error, however it should be something like
Expand|Select|Wrap|Line Numbers
  1. data->time = (float *)malloc(sizeof(float) * data->points);
  2.  
to get an array of the correct size.
Sep 24 '07 #2
Thanks for your reply, I'll try that and get back to you on it.
Sep 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
12
by: Mike | last post by:
Consider the following code: """ struct person { char *name; int age; }; typedef struct person* StructType;
9
by: zholthran | last post by:
Hi folks, after reading several threads on this issue (-> subject) I fear that I got a problem that cannot easily be solved by the offered workarounds in an acceptable way, at least not with my...
6
by: Roman Mashak | last post by:
Hello, I belive the reason of problem is simple, but can't figure out. This is piece of code: struct timeval { long tv_sec; /* seconds */ long tv_usec; /* microseconds */ };
8
by: Ivan Liu | last post by:
Hi, I'd like to ask if passing an object as an pointer into a function evokes the copy constructor. Ivan
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
40
by: Angus | last post by:
Hello I am writing a library which will write data to a user defined callback function. The function the user of my library will supply is: int (*callbackfunction)(const char*); In my...
18
by: sanjay | last post by:
Hi, I have a doubt about passing values to a function accepting string. ====================================== #include <iostream> using namespace std; int main() {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.