I will have two questions . First I havent got a enough information about the funcitons : malloc,calloc,realloc. Can anyone show me a direction to a source or can anyone directly explain their jobs detailed?
Secondly , does the vary the sscanf function returns depends on the how many variable it used? By variable I mean the variables that we save the varies that we took from the string we put into the first argument of sscanf function.
I hope I made my questions clear. I will be glad if you help. Thank you..
(for example sscanf(s,"%d",&a)==1 ??? )
Real brief (and from memory, so check what I'm saying yourself):
Allocates contiguous memory in terms of bytes. Accepts a size_t as an argument and returns a void *. If the memory could not be allocated, the return value is NULL.
Similar to malloc(), with a few small changes. It accepts two args, one being the size of one element, and the other being the amount of elements. Another difference is that calloc() initializes all bytes of the memory it allocates to 0. As with malloc(), if the memory could not be allocated, NULL is returned.
This function is used to change the size of a block of memory allocated by either malloc(), calloc(), or realloc(). You pass it the pointer to the original block of memory, and specify the new size. If it is able to perform the memory resize, it returns a pointer to the new block of memory. If it couldn't resize the old block, it'll try to make a new block and copy all of the data from the old block to the new block. It then frees the old block. If the pointer you give it to resize is NULL, it behaves similar to malloc(). If it couldn't manage to resize the block of memory, or create a new block upon receiving a NULL pointer, it returns NULL.
As far as sscanf() and the scanf() family of functions, yes, the return value is the number of variables that received valid values. If this return value is not equal to the number of variables you gave it, something went wrong with the assignment of at least one variable (or something major went wrong).
Yes, sscanf(s, "%d", &i) would return 0 if s doesn't contain a number, 1 if there is a number in s. Note also that i will only be filled in if it returns one.
sscanf(s, "%d %d %d", &i, &j, &k) could return any number between 0 and 3, depending on what's in s.
I think your hopes didn't come out true.
The question is kinda not clear to me.
for the 1st question refer the man pages, if you dont know what they are then search Google may be the search query "malloc man page" and see the links, you'll get all you need.
Yea zombie I did that before , then I got the teorical base , I just wanted to know if there are more about them...Thank you all , you clarified all my questions..
So , I wonder if I do this :
output is 10?? Or I didnt get anything right?
Depending on your computer, the output would most probably be 4.
You're asking for the sizeof the pointer, not what it points to.
yes it should give you 4 as array is the pointer and the sizeof operator when applied on a pointer shall return you 4 on the machines we normally use today.
I am sorry It should have been
That will print 1 because a char is one byte.
But I did array=malloc(10*sizeof(char)) before I printed the sizeof(*array) .. Dint I allocate a 10 char memory to array ? So shouldnt it print out 10??
But Is it because When I do that I set a 10 lenght string and the array points to the first component of the string and that is 1 byte?
Also I wonder :
I deliberately use gets to lead program to an error. But I enter an input more than 10 character , it does not give error. What is the problem. I think I couldnt get what malloc does..
gets does not check the buffer size...
it starts from the first byte pointed by its argument and writes till the user input end...
that's why you should not use it - use fgets as FAQ suggests
Vart you are not serious are you? Dont you read people's questions? Or are you ??????!!!!! Please read my lines carefully..! What did I say "I DELIBERATELY used gets to lead program to an error" ... So what are you talking about ? Dont walk aroun with the crave of teaching people ...
you make an error - you write to the memory you do not own.
You do not get crash - you're lucky...
Next time - pay attention - to what is talled, and be more polite in your talking