Solved

Allocation memory

Posted on 1998-06-11
5
237 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
  • 3
  • 2
5 Comments
 

Author Comment

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

Accepted Solution

by:
nietod earned 100 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now