dynamically defined array size in a function

This is a discussion on dynamically defined array size in a function within the C Programming forums, part of the General Programming Boards category; If I don't typecast I get this error: Code: '=' : cannot convert from 'void *' to 'long *' for ...

  1. #16
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    If I don't typecast I get this error:
    Code:
    '=' : cannot convert from 'void *' to 'long *'
    for this function:
    Code:
    long *TempValue;
    TempValue = (long *)calloc(*n, sizeof(long));
    and later when I try putting values in there I get another error message:
    Code:
    fread' : cannot convert parameter 1 from 'long' to 'void *'
    from this call:
    Code:
    fread(TempValue[i], sizeof(long), 1, ptr);
    and I know I put & everywhere I'm not supposed to. I don't understand very well where to and not to put it. I'm still trying to figure it all out.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop compiling as C++ then and compile it as C.

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

  3. #18
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Seriously, you've been told multiple times in this thread to stop compiling your program as C++. When will you listen?
    If you understand what you're doing, you're not learning anything.

  4. #19
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    I'm using a C++ compiler. Running the .c file gives me 62 errors. It compiles almost ok as a .cpp.

  5. #20
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by earth_angel
    I'm using a C++ compiler. Running the .c file gives me 62 errors. It compiles almost ok as a .cpp.
    You're learning things the wrong way and using kludges and band-aids in your code. Please start listening to the advice here.

    In C(89), structs aren't automatically typedefed and you cannot declare variables all over the place like you can in C++. Add the struct keyword where necessary or create a typedef, declare the variables at the top of the block, compile as C, and then see if you can't get rid of the errors by writing correct code, rather than obscuring the mistakes by intermixing the languages.
    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.*

  6. #21
    Registered User
    Join Date
    Mar 2005
    Posts
    36
    To start over, you can't allocate space usefully until you know how much spoace you want to allocate. This means that you can't just allocate at compile time unless you at least know a safe maximum. You may know (A) how many fields, or (B) how big they are without knowing both - or may know (C) neither, each field may be a different length with no known maximum.

    (A) You can allocate a pointer table of fixed size, and allocate space for each field as you find it, store the data in it and assign its value to one of the pointers in the table.

    (B) You can allocate for several fields, fill them, reallocate for more empties and fill them, etc - then address them as an indexed table of fixed length fields.

    (C) You can build a linked list of allocated spaces. Like so, will work, maybe what you are looking for:
    Code:
    struct _mystruct {
        struct _mystruct* next;
        char data[1];
    };
    struct _mystruct* first, last;
    
    void store_this ( char* string ) {
    
        struct _mystruct* temp;
    
        temp = malloc( sizeof(struct _mystruct) + strlen(string) );
        if (!temp) {
            cry_wolf();
            exit(1);
        }
        strcpy(temp->data,string);
        if (!first) first = temp;
        else last->next = temp;
        last = temp;
    }
    
    void print_them ( )
    {
        for( last = first; last ; last=last->next )
             printf("%s\n",last->data);
    }
    This will work because the compiler will not complain that you are overstepping the end of the data array in the struct and since it is at the end you just need to alocate enough space
    following to hold the second, third, etc characters and the nul char.

    After the list is built, the "last" variable is not needed, you can tell when you hit the tail entry because it has an empty (NULL) "next" ptr.

  7. #22
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,497
    Code:
    struct _mystruct {
        struct _mystruct* next;
        char data[1];
    };
    1. Names beginning with underscores are reserved for the implementation.

    2. The legality of your array depends on your compiler
    In C89 - http://www.eskimo.com/~scs/C-faq/q2.6.html
    In C99 - http://david.tribble.com/text/cdiffs.htm#C99-fam
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 2 of 2 FirstFirst 12
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, 01:39 PM
  3. DLL compiling question
    By Noose in forum Windows Programming
    Replies: 2
    Last Post: 12-16-2004, 06:16 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 09: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21