size of array

This is a discussion on size of array within the C Programming forums, part of the General Programming Boards category; And, this is incorrect syntax: Code: printf("Please enter the size of the array" , size );...

  1. #16
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    And, this is incorrect syntax:
    Code:
     printf("Please enter the size of the array", size);
    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!

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

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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
    21,720
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    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?

Page 2 of 3 FirstFirst 123 LastLast
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, 10:27 AM
  2. Replies: 42
    Last Post: 12-19-2004, 07: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, 12:01 AM

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