#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep 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
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    6
    Rep 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.
  4. #3
  5. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo