The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
C - Using Malloc
Discuss C - Using Malloc in the C Programming forum on Dev Shed. C - Using Malloc C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 2nd, 2012, 04:00 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 6
Time spent in forums: 48 m 23 sec
Reputation Power: 0
|
|
|
C - Using Malloc
I'm trying to copy from a file. The structure of the file is the first line is 2 ints (to give the size needed for storing the remaining floats in memory) and a list of floats.
I am using findvals -r float -f tolerance < mat.10x20 > output
The user should be able to specify a float, and a tolerance, and the program should compare to the file and output the number of valid numbers to another file. Just doesn't seem to be working and I'm stumped! Would be so much easier in Java!!
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utils.h"
int main(int argc, char* argv[])
{
if (strcmp(argv[1],"-r") != 0 || strcmp(argv[3],"-f") != 0)
{
perror(argv[1]);
exit(1);
}
else if (strcmp(argv[3],"-f") != 0 || strcmp(argv[1],"-r") != 0)
{
perror(argv[3]);
exit(1);
}
else
{
}
int rcount, ccount; //number of rows and columns.
float referenceNum = strtof(argv[1],0); // defines the number looking for
float toleranceNum = strtof(argv[2],0); // defines the tolerance accepted
scanf("%d %d", &rcount, &ccount);
float** rows = malloc(rcount * sizeof(float *));
if (rows == 0)
{
fprintf(stderr, "Couldn't alocate sufficient space.\n");
exit(1);
}
int i;
for (i = 0; i < rcount; i++)
{
float* row = malloc(ccount * sizeof(float));
if (row == 0)
{
fprintf(stderr, "Couldn't alocate sufficient row space.\n");
exit(1);
}
rows[i] = row;
}
int x, y;
for (x = 0; x<rcount; x++)
{
for (y = 0; y<ccount; y++)
{
scanf("%f", &rows[x][y]);
}
}
for (x = 0; x<rcount; x++)
{
for (y = 0; y<ccount; y++)
{
printf("%f", rows[x][y]);
}
}
exit(0);
}
This would be mat.2x3
2 3
68.673767 11.473727 87.138351
51.231050 -25.041696 -33.239058
|

November 2nd, 2012, 04:01 AM
|
|
Registered User
|
|
Join Date: Oct 2012
Posts: 6
Time spent in forums: 48 m 23 sec
Reputation Power: 0
|
|
|
Oh, I don't have the code implemented for comparison yet, and I don't think I'm getting it into memory correctly as the printf should be putting the whole file into an output file without checking, but that doesn't seem to be working.
|

November 2nd, 2012, 04:32 AM
|
 |
Contributed User
|
|
|
|
|
There is nothing wrong with the malloc code, but the logic for testing argv parameters needs work.
> if (strcmp(argv[1],"-r") != 0 || strcmp(argv[3],"-f") != 0)
If argv[1] is "-r", then the left side evaluates to false, and the right side evaluates to true.
false || true is true.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|