Thread: calloc

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    calloc

    i am trying to make an array where user choses the size, and i dont seen able to get it right. What am I doing wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double* makeAnArray(double size);
    	int main (){
    	int anArray()={0};
    	int size=0;
    
    	printf("Please enter the size of the array");
    	scanf("%i", &size);
    
    	makeAnArray(size);
    
    	free(anArray);
    
    	getchar();
    }
    double* makeAnArray(double size){
    	int* array;
    	array =calloc(size,sizeof(double));
    	return *array;
    }

  2. #2
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    This will compile. Start here:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double* makeAnArray(double size);
    
    int main (void)
    {
    	int anArray[]={0};
    	int size=0;
    
    	printf("Please enter the size of the array");
    	scanf("%i", &size);
    
    	makeAnArray(size);
    
    	free(anArray);
    
    	getchar();
    	
    	return 0;
    }
    double* makeAnArray(double size)
    {
    	double* array;
    	
    	array =calloc(size,sizeof(double));
    	
    	return array;
    }

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i fixed
    Code:
    int anArray[]={0}
    in the declarations
    now when i run the program i get an error msg what is wrong with it?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Go back to basics. You still have understood nothing of returning and freeing nor arrays nor type nor calloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    	int anArray()={0};
    I'm pretty sure this doesn't compile. It sort of declares a function, then attempts to initialize it zero to it. If this actually compiles, I'd complain to whoever produces the compiler.

    Code:
    	makeAnArray(size);
    You are not receiving the return value into a variable.

    Code:
    	free(anArray);
    Since anArray is not correctly defined, this line will also fail.

    Perhaps you can try to compile the code and then ask questions when you either can't fix the compiler errors, or when you have code that compiles but perhaps doesn't do what you want.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i made all the corrections and it is still not working
    this is the errror that i get
    Windows has triggered a breakpoint in Assignment 8.1.exe.

    This may be due to a corruption of the heap, which indicates a bug in Assignment 8.1.exe or any of the DLLs it has loaded.

    This may also be due to the user pressing F12 while Assignment 8.1.exe has focus.

    The output window may have more diagnostic information

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    If you made all the corrections, then it would work.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i have been through my book and that was no help at all.
    from what i get from it is that the array size has to be declared at he beggining and cannot be changed, but how am i supposed to have the user enter the size then???

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Well, there is one error we didn't tell you about today, but we did tell you about it yesterday.

    Code:
    int size=0;
    ...
    scanf("&#37;i", &size);
    ...
    makeAnArray(size);
    ...
    double* makeAnArray(double size)
    Make size an int, not a double.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by goran00 View Post
    i have been through my book and that was no help at all.
    from what i get from it is that the array size has to be declared at he beggining and cannot be changed, but how am i supposed to have the user enter the size then???
    That's why you use dynamic memory!
    And we have told you multiple times that you need to receive the return from the function and then free it! But you aren't listening!
    Perhaps you need to check out tutorials or better books.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Nub SWE
    Join Date
    Mar 2008
    Location
    Dallas, TX
    Posts
    133
    Quote Originally Posted by Elysia View Post
    That's why you use dynamic memory!
    And we have told you multiple times that you need to receive the return from the function and then free it! But you aren't listening!
    Perhaps you need to check out tutorials or better books.
    Honestly, some new eyeglasses wouldn't hurt.

    Oh, and the ability somehow to take the help that is given and put it to good use.

  12. #12
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i have been strugling with this for quite a few days now. This is my first time using c so I am having an extremely hard time with it. I understand that you have been telling me the same thing yesterday and today, but certain things i do not understand and do not know how to do them. and thats why I am trying to get some more help.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    And that's why we're telling you to read more about the basics. Otherwise you're just going to get stuck somewhere else.
    A very, very, very basic task which we have told you again and again is to take the return value of the function. Then you can free it. I believe we've even shown you examples.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    Quote Originally Posted by Elysia View Post
    take the return value of the function. Then you can free it.
    I have searched online read books and I can not figure out how to this.
    can you shouw me an example of it. I do not recall an example of this.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Also, using dynamic memory allocation is a useful thing to understand, but MANY problems can be solved with a sufficiently large statically sized array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc, calloc from the FAQ
    By salvadoravi in forum C Programming
    Replies: 10
    Last Post: 01-21-2008, 03:29 AM
  2. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  3. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  4. Why use calloc()?
    By dwks in forum C Programming
    Replies: 8
    Last Post: 07-20-2005, 08:22 AM
  5. difference between calloc and malloc
    By saravanan_ts in forum C Programming
    Replies: 4
    Last Post: 07-28-2003, 06:13 AM