Thread: dynamically defined array size in a function

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    dynamically defined array size in a function

    I'm writing a function to read in a number of words from a binary file. the size of each word and the number of words to be read are defined by the function input parameters. I cannot allocate the size of the array to contain all th words properly.

    Code:
    void ReadSW01HEAD(FILE *ptr, long n /*Number of Parameters*/, long Size /*Section Size*/)
    {
    	char Param[n];
    	int FieldLength = Size/n; 
    	
    	printf("Parameters Present\n\n");
    
    	fread(&Param,sizeof(char),FieldLength,ptr);
    	printf("%s   ",Param);
    }
    Do I need a pointer of some sort?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    When asking a question, try to say what the program is doing rather than what it it's not doing.

    Bad:
    I cannot allocate the size of the array to contain all th words properly.
    Good:
    It gives me the error message "<error message here>" when I try to allocate the size of the array to contain all the words properly.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    The error message that I get is:
    Code:
    error C2133: 'Param' : unknown size

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It looks like you're using a compiler that doesn't support C99. You can get around the limitation by using malloc() to get memory for Param:
    Code:
    char *Param = malloc(n);
    Don't forget to free(Param); when you're doing using that memory.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Code:
    void ReadSW01HEAD(FILE *ptr, long n /*Number of Parameters*/, long Size /*Section Size*/)
    {
    	char *Param = malloc(n);
    	int FieldLength = Size/n; 
    	
    	printf("Parameters Present\n\n");
    
    	fread(&Param,sizeof(char),FieldLength,ptr);
    	printf("%s   ",Param);
    }
    Gives me an error message like this:

    Code:
    error C2440: 'initializing' : cannot convert from 'void *' to 'char *'
            Conversion from 'void*' to pointer to non-'void' requires an explicit cast
    Error executing cl.exe.
    Why is it asking for a cast?

  6. #6
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Ignore last post, this corrected code seems to do the trick.

    Code:
    void ReadSW01HEAD(FILE *ptr, long n /*Number of Parameters*/, long Size /*Section Size*/)
    {
    	char *Param;
    	int FieldLength = Size/n; 
    
    	Param = (char *) calloc(n, FieldLength);
    	
    	printf("Parameters Present\n\n");
    
    	fread(&Param,sizeof(char),FieldLength,ptr);
    	printf("%s   ",Param);
    }

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It looks like you're using a C++ compiler to compile C code. The "broken" malloc() code was actually the correct one. Your compiler is just wrong.

    But you don't want to use fread(&Parm,...). fread(Param,...) is correct.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Another problem with the calloc:

    Once I try to write to the array elements, an error message pops up saying that the <memory could not be"read">. Is the syntax incorrect?

  9. #9
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Can I use calloc inside a structure, ie:

    Code:
    struct ParamName    // An array of char for one param name
       {
    	   char *Name;
    	   Name = (char *)calloc(NameLength, sizeof(char));
    	   
       };
    and then

    Code:
    ParamName Params[NumParam];
    Right now it's giving these errors:

    Code:
    illegal pure syntax, must be '= 0'
    H:\Code\Complete File Parcel\Main Fn.cpp(117) : error C2501: 'Name' : missing storage-class or type specifiers
    H:\Code\Complete File Parcel\Main Fn.cpp(122) : error C2057: expected constant expression
    H:\Code\Complete File Parcel\Main Fn.cpp(122) : error C2466: cannot allocate an array of constant size 0
    H:\Code\Complete File Parcel\Main Fn.cpp(122) : error C2133: 'Params' : unknown size

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    No, you can't. And you're still compiling your C program as C++. If you're going to be programming in C, compile it as C. C and C++ are not the same language.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just save to file as a .c file and it should compile as a C program.

  12. #12
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    Is there any way to dynamically allocate an array inside a structure.

    Code:
     struct ParamName    // An array of char for one param name
       {
    	   char Name[8];
       };
    If say [8] is not always 8 and it is a variable.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct ParamName
    {
       char *Name;
    };
    
    int main(void)
    {
       struct ParamName param;
       param.Name = malloc(8);
       if ( param.Name )
       {
          strcpy(param.Name, "Hello");
          puts(param.Name);
          free(param.Name);
       }
       return 0;
    }
    
    /* my output
    Hello
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I've gottna so confused with the proper variable declarations. here's the complete chunk of the program with the array structure in it. It gives a run-time error : memory could not be 'written'

    Code:
    struct ParamName    // An array of char for one param name
       {
    	   char *NameP;  
       };
                            // Allocate the array of size nameLengthP
       ParamName SizedParameterName;
       SizedParameterName.NameP = (char *)calloc(NameLengthP, sizeof (char));
    
       int j;
    
       ParamName *Params;
       Params = (ParamName *) calloc(NumParam, NameLengthP); // An array of names for all of the param's
    
       for(i=0;i<NumParam;i++)
       {
    	   for(j=0;j<NameLengthP;j++)
    	  	   fread(&Params[i].NameP[j],sizeof (char),1,inp);
    		   
       }

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop typecasting malloc / calloc. Read the FAQ is you want to know why it's wrong, I'm getting tired of explaining to everyone.

    Next make a nice little test program to get what you're doing working:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAGICNUMBER 5
    
    struct foo
    {
        char *bar;
    };
    
    int main( void )
    {
        struct foo *array;
        int x;
    
        array = malloc( MAGICNUMBER * sizeof( struct foo ) );
        if( array == NULL )
        {
            printf( "malloc failure\n" );
            return 0;
        }
    
    
        for( x = 0; x < MAGICNUMBER; x++ )
        {
            array[ x ].bar = malloc( strlen( "Hello World!" ) + 1 );
            strcpy( array[ x ].bar, "Hello World!" );
        }
    
        for( x = 0; x < MAGICNUMBER; x++ )
        {
            printf("array[ %d ].bar = %s\n", x, array[ x ].bar );
            free( array[ x ].bar );
        }
        free( array );
    
        return 0;
    }
    The main problem I think you're having is that you keep using the & operator when you shouldn't be.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. Linking problems in Visual Studio
    By h3ro in forum C++ Programming
    Replies: 5
    Last Post: 03-04-2008, 02:39 PM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 07:16 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM