Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Doubt in C programming

Status
Not open for further replies.

navenmou

Full Member level 4
Joined
Sep 25, 2010
Messages
228
Helped
49
Reputation
98
Reaction score
46
Trophy points
1,318
Location
Bangalore, India
Activity points
2,588
hiii all

i have few doubts in c programming

1.what is difference between macro and typedef

2.why do we need typecast for malloc functrion

3.what is pointer to an array and array of pointers
 

1.what is difference between macro and typedef

The purpose of typedef is to assign alternative names to existing types, for example typedef long int lint, after that you can declare lint my_longint and it will be the same as long int my_longint
A macro is a fragment of code which has been given a name, whenever the name is used, it is replaced by the contents of the macro for example #define led_on PORTA|=0x04; or you can do functions like #define calc(x,y,z) (x*y)+z;
Then when you write led_on it will be replaced with PORTA|=0x04;
and when you write calc(1,2,3) it will be the same as (1*2)+3;

3.what is pointer to an array and array of pointers

a pointer to an array of two char is char (*my_array_pointer)[2], this is one pointer that points to the starting address of an array of char, then you can read the array using *my_array_pointer)[0], *my_array_pointer)[1].

An array of pointers is char *my_pointer_array[2], this has 2 pointers that can point in two different addresses.

2.why do we need typecast for malloc functrion

I don't use malloc but I think this will answer your question Cprogramming.com FAQ > Casting malloc

Alex
 
i thinik the malloc function will return a void pointer. So we need to type cast into required user defined type.
 

i thinik the malloc function will return a void pointer. So we need to type cast into required user defined type.

The link that I have posted in my previous post ( Cprogramming.com FAQ > Casting malloc ) says

Originally, the C language did not enjoy the void pointer. In those dark ages the char pointer was used as a generic pointer, so the definition of malloc looked something like this:

char *malloc(size)
int size;

Of course, this tended to cause problems when trying to assign a char pointer to, say, a double pointer. Because of this a cast was required:

double *p;
p = (double *)malloc ( n * sizeof ( double ) );

However, with the advent of the ISO C standard a new generic pointer was created, the void pointer. A void pointer can be assigned to any pointer type (except function pointers) safely and without the need for a cast. Thanks to this, the calls to malloc were simplified:

double *p;
p = malloc ( n * sizeof ( double ) );

However, many still prefer to cast malloc because they feel it makes the intentions of the memory allocation more clear. There is nothing wrong with this except in the event that stdlib.h, the header which declares malloc, is not included. If the return of malloc is cast then the error which would be flagged is hidden, resulting in a difficult to find bug. Also, during maintenance, if the type of the pointer changes but the cast is not changed, once again there is a difficult to find bug. The method most experienced programmers choose is:

p = malloc ( n * sizeof *p );

There is no cast for malloc since there is no need for one, and instead of using sizeof ( type ) to determine the size of the block, sizeof *ptr is used. By dereferencing the pointer and taking its size, the proper value is given without having to worry about modifying the allocation request if the type of the pointer changes.

So should you cast malloc? No, that is the preferred method. However, you should use whichever you like provided you are aware of the issues.


Alex
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top