Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A minor confusion in C

Tags:

c

pointers

i am new to C programming .... so there are mere chances you will find the question damn easy

char *ii;
int i=54;

ii=(char *)&i;

for what purpose is there (char *) and is using char pointer for int address is helpful in some way ? and is it will be exactly the same as with using int pointer ?

like image 892
parth_07 Avatar asked Feb 10 '23 12:02

parth_07


1 Answers

This question seems to assume that using a char* to hold the address of an integer has a valid purpose.

It does not, and is not a proper way to code.

There are cases where the data that a pointer points to is not known at compile time. And that can lead to code similar to this. But, as a general rule, it is not useful and should be avoided.

like image 109
Jonathan Wood Avatar answered Feb 13 '23 03:02

Jonathan Wood