Calloc C++


 
Thread Tools Search this Thread
Top Forums Programming Calloc C++
# 1  
Old 08-04-2011
Calloc C++

Hello all,

I have a confusion with calloc function :

wz. the difference between the following 2 statemnts:

HTML Code:
char *ptr;
 
char = (char*)calloc(num, sizeof(char));
 
char = (char*)calloc(num, sizeof(char*));
Am really confused!!!!!

---------- Post updated at 09:32 AM ---------- Previous update was at 09:29 AM ----------

Sorry they are :

HTML Code:
ptr = (char*)calloc(num, sizeof(char));
 
ptr = (char*)calloc(num, sizeof(char*));
# 2  
Old 08-04-2011
Well, the obvious thing is to check:

Code:
printf("char = %d\n", sizeof(char));
printf("char * = %d\n", sizeof(char *));

Code:
char = 1
char * = 4

You've discovered the difference between a character, and a pointer to a character. The pointer is a memory address (4 bytes on 32-bit x86) that can be used to point to other memory.

Since you've declared a char *, it's a safe bet you want a pointer to characters, not a pointer to pointers, so use the first one.
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Programming

calloc fails: 'Cannot allocate memory'

Hi , experts. I work on Linux station (RedHat 5.7), regular user, but have root password. %> uname -a Linux ran1log06 2.6.18-238.1.1.el5 #1 SMP Tue Jan 4 13:32:19 EST 2011 x86_64 x86_64 x86_64 GNU/Linux %> cat /etc/issue Red Hat Enterprise Linux Client release 5.7 (Tikanga) Kernel \r on... (5 Replies)
Discussion started by: baruchgu
5 Replies

2. Programming

Iterating and calloc questions.

Whilst creating the function readjust_descr I have stumble across what may be a problem or something that might just work. I was hoping someone could look at the code below and tell me if readjust_descr will clear all null pointers from the structure descr_list. struct descr descr_list =... (6 Replies)
Discussion started by: Errigour
6 Replies

3. UNIX for Advanced & Expert Users

memset vs calloc

Dear Friends, Can any one tell me the difference between memset and calloc function in C. Regards, Selvi (7 Replies)
Discussion started by: salvi
7 Replies
Login or Register to Ask a Question