Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Allocation memory

Posted on 1998-06-11
5
Medium Priority
?
245 Views
Last Modified: 2010-04-01
What does the (char *) do in this sample program?

#include<stdlib.h>

main()
{
char *str;
if ((str= (char *) malloc(100)) ==NULL)
 {
   printf("not enough memory to allocate buffer\n");
   exit(1);
 }
printf("String was allocated");
return 0;
}
0
Comment
Question by:program
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 

Author Comment

by:program
ID: 1165701
Edited text of question
0
 
LVL 22

Accepted Solution

by:
nietod earned 400 total points
ID: 1165702
answer coming.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165703
The return value of malloc() is a pointer of type "void *".  This means it doesn't point to any defined type.  This is because malloc doesn't know what you want to use the memory for, so it returns this, sort of, generic pointer.    you must convert this pointer to something useful.  You do this using a type cast, which is to put a type name inside of parenthesis before the thing to be changed.  For example.

int i;
char *CPtr1  = &i; // error &i is an int* pointer, not a char * pointer.
char *CPtr2 =  (char *) &i; // Okay converted from int * to char *.

you can also use this with non-pointers, like

double D = 5;
char c = (char) D; // convert D to a char.

0
 

Author Comment

by:program
ID: 1165704
Thanks for the answer.  This question stumped many people at my
office who have C knowledge.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1165705
One thing I should have said and didn't is that the new operator is type safe and malloc is not.   That is new creates memory of a particular type and returns a pointer to that type.  This is much better than malloc.  A C++ program should never need to use malloc()  (except in the case of implimenting operator new).
0

Featured Post

Tech or Treat!

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question