Reputation: 91
I'm nearing the end of an introductory book on c programming called "C programming in easy steps" by Mike McGrath. I think I'll have a lot more to learn after this book by the looks of things though. Anyways, I'm working with memory allocation and I wrote this demo program, but it errors closed when I try to run it:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, *arr;
arr = calloc(5, sizeof(int));
if(arr!=NULL)
{
printf("Enter 5 integers, seperated by a space:");
for(i=0; i<5; i++) scanf("%d", &arr[i]);
printf("Adding more space...\n");
arr = realloc(8, sizeof(int));
printf("Enter 3 more integers seperated by a space:");
for(i=5; i<8; i++) scanf("%d", &arr[i]);
printf("Thanks.\nYour 8 entries were: ");
for(i=0; i<8; i++) printf("%d, ", arr[i]);
printf("\n");
free(arr);
return 0;
}
else {printf("!!! INSUFFICIENT MEMORY !!!\n"); return 1; }
}
Warning message:
|13|warning: passing argument 1 of 'realloc' makes pointer from integer without a cast|
c:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.4.1\..\..\..\..\include\stdlib.h|365|note: expected 'void *' but argument is of type 'int'|
||=== Build finished: 0 errors, 1 warnings ===|
The resulting compilation asks for 5 integers and prints "Adding more space..." at which point the program terminates, instead of asking for the additional three integers and printing the input.
Any help would be nice. :) Thanks!
Upvotes: 1
Views: 3542
Reputation: 9393
At fist You gotta learn the the way to use realloc. The reason for the 1st warning your seeing on your screen
passing argument 1 of 'realloc' makes pointer from integer without a cast
int i, *arr;
arr = calloc(5, sizeof(int)); \\ Not the right way
arr = (int *) calloc (5,sizeof(int)); \\ makes sense
You have to type cast the pointer returned by The calloc before it is being assigned.Thats why you got the warning without a cast. Note
char *arr ; arr = (char *) realloc (5,sizeof(char))
double *char: arr =(double *) realloc (5,sizeof(double))
I guess you got the point.Typecast it into the type of the pointer your going to assign. because the calloc returns void pointer and you have to type cast into the data type you want before you use it
and As far to my Knowledge
for(i=0; i<5; i++) scanf("%d", &arr[i]); \\ this is not the way to use pointers
this would be mostly used in arrays !
This is the way to use pointers
*(arr+1) or *(arr+any_variable) \\
and remember as your defining arr as integer pointer it increments its address by 2
example arr pointing to 3000 memory location after *(arr+1)
points to 3002 location and if arr pointer is ,char *arr, then
arr pointing to 3000 then *(arr +1 ) now it will point to 3001 location
same for double by 8 and for float 4 .
1 does not mean by 1 it means increment it by the size of the pointer pointing to. well I never noticed this kind of syntax for pointers
&arr[i]
If that is also the way to use pointers then I'm glad to accept it I learned the other way to access pointers
but think of using these *(arr+i) mostly
well you can also use
(int *) calloc(5, sizeof(int))
\\ the number of bytes allocated by it is 5 * sizeof(int) = 5*2 =10
Thanks.
and please go through these http://www.cplusplus.com/reference/clibrary/cstdlib/calloc/ and these site is really really promsising site for beginners I hope you enjoy and any doubts we are here to help you
Upvotes: -1
Reputation: 14535
Your use of realloc
is wrong.
// ptr: Pointer to a memory block previously allocated with malloc, calloc or
// realloc to be reallocated.
void * realloc ( void * ptr, size_t size );
You should use realloc(arr, 8 * sizeof(int));
Upvotes: 0
Reputation: 477494
Look up how realloc
works - you have to pass it the original pointer:
int * tmp = realloc(arr, 8 * sizeof(int));
if (tmp)
{
arr = tmp;
}
else // Error!
Upvotes: 0
Reputation: 52549
The first argument to realloc
should be the previously allocated pointer
Upvotes: 0
Reputation: 300749
You need to pass the pointer to the memory you want to resize:
tmp = realloc(arr, 8 * sizeof(int));
if (NULL != tmp)
arr = tmp;
Upvotes: 1
Reputation: 182714
You're not using realloc
the way you should:
/* 8 is not valid here. You need to pass the previous pointer. */
arr = realloc(8, sizeof(int));
Try:
tmp = realloc(arr, 8 * sizeof(*arr));
if (NULL != tmp)
arr = tmp;
As an aside, your program looks really crammed which makes it hard to read. Maybe leave an empty line once in a while ?
Upvotes: 7