Thread: size of array

  1. #16
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    And, this is incorrect syntax:
    Code:
     printf("Please enter the size of the array", size);
    Mainframe assembler programmer by trade. C coder when I can.

  2. #17
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    would free(array); be in main or function?

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Once you free the memory, you can't use it anymore. So it must be done when you're finished with it. What does that tell you?
    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.

  4. #19
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    using system("pause") and gerchar() is that the same thing?
    does the program stop so that I can see whats on the screen?

  5. #20
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes.
    This is just one of many methods to do it. If you have a debugger, you can just set a breakpoint at the last line.
    In Visual Studio, you can run without debugging and it will hold the window open.
    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.

  6. #21
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    thanks

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    this is what i have so far and there is an enternal error
    what am i doing wrong?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int* makeAnArray(int);
    main (){
    int size=0;
    
    printf("Please enter the size of the array", size);
    scanf("&#37;i", &size);
    
    makeAnArray(size);
    
    array(free);
    
    getchar();
    }
    int* makeAnArray(int size){
    	int* array;
    array=calloc(size,sizeof(int));
    return array;
    }

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Huh? You should be calling free with array as an argument, not call a function array() with the argument free, first of all.

    Second, you are not "receiving" the array returned by makeAnArray() [as I pointed out in a previous post].

    --
    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.

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I repeat:
    Code:
    int main (){
    Code:
    printf("Please enter the size of the array");
    And then:
    Code:
    int* makeAnArray(int size);
    Don't omit variable names in prototypes.

    And lastly:
    Code:
    makeAnArray(size);
    array(free);
    You let the return (your pointer!) from makeAnArray go lost. Without a trace. Thus the memory is leaked. Never to be found ever again until your app exits. Very bad.
    And what's "array(free)"?

    You free using the function free().
    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.

  10. #25
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int* makeAnArray(int size);
    int main (){
    	int size=0;
    
    	printf("Please enter the size of the array");
    	scanf("&#37;i", &size);
    
    	makeAnArray(size);
    
    	free(makeAnArray);
    
    	getchar();
    }
    int* makeAnArray(int size){
    	int* array;
    	array=calloc(size,sizeof(double));
    	return array;
    }
    here is what i have. i am still getting major error????

  11. #26
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Do tell how you're supposed to free a function?
    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.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to assign the pointer returned by makeAnArray() to a pointer to int, and then free that pointer to int. Also the second argument to calloc() should be a sizeof(int), not sizeof(double).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    the reason i put sizeof(double) is because the numbers that will be put into the array will be doubles. is that incorrect?

  14. #29
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Then you need to allocate a pointer to double! You can't allocate int and use doubles!
    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.

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    Quote Originally Posted by laserlight View Post
    You need to assign the pointer returned by makeAnArray() to a pointer to int, and then free that pointer to int. Also the second argument to calloc() should be a sizeof(int), not sizeof(double).
    i am not understand this, i understand what you are saying but where am i making the mistake?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Invalid conversion from 'void*' to 'BYTE' help
    By bikr692002 in forum C++ Programming
    Replies: 9
    Last Post: 02-22-2006, 11:27 AM
  2. Replies: 42
    Last Post: 12-19-2004, 08:59 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM