Thread: size of array

  1. #31
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    what would i have to keep as an int since size is going into the function as an int???
    or could i just make everyhting a double?

  2. #32
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    makeAnArray returns a value. You aren't storing it in a variable.
    If you want to work with doubles, then allocate doubles.
    If you want to work with integers, then allocate integers.
    Don't do anything else. Change the function accordingly.
    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.

  3. #33
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i changed everything over to doubles
    not when i run the program i get this error box:
    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.

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

  4. #34
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by goran00 View Post
    i changed everything over to doubles

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double* makeAnArray(double size);
    int main (){
    	int size=0; 
    
    	printf("Please enter the size of the array");
    	scanf("%i", &size);
    
    	makeAnArray(size);
    
    	free(makeAnArray);
    
    	getchar();
    }
    double* makeAnArray(double size){
    	int* array;
    	array=calloc(size,sizeof(double));
    	return array;
    }
    No you didn't.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #35
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double* makeAnArray(double size);
    int main (){
    	int size=0;
    
    	printf("Please enter the size of the array");
    	scanf("%i", &size);
    
    	makeAnArray(size);
    
    	free(makeAnArray);
    
    	getchar();
    }
    double* makeAnArray(double size){
    	int* array;
    	array=calloc(size,sizeof(double));
    	return array;
    }
    What the red bit does is give the function free() the address of makeAnArray - that is not a piece of memory that has been allocated through malloc/calloc, so it will not "go down well".

    --
    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. #36
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Again, you can't free a function!
    makeAnArray is a function!
    You didn't allocate it with malloc or calloc, so you can't free it!
    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.

  7. #37
    Registered User
    Join Date
    Jan 2008
    Posts
    124
    i changed that to free(array)
    but the problem i am having with that is
    error C2072: 'array' : initialization of a function
    here is the code. How can i fix that
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    double* makeAnArray(double size);
    	int main (){
    	int array()={0};
    	int size=0;
    
    	printf("Please enter the size of the array");
    	scanf("&#37;i", &size);
    
    	makeAnArray(size);
    
    	free(array);
    
    	getchar();
    }
    double* makeAnArray(double size){
    	int* array;
    	array =calloc(size,sizeof(double));
    	return *array;
    }

  8. #38
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    OMG. I'm totally invisible today. Why am I wasting my time.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #39
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indeed. You need to go back to basics. You don't seem to understand much about C, at all.
    Go back to reading about variables, functions, arguments and return types. Then try again.
    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.

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