简体   繁体   中英

In C copy data defined in code into a memory location of a char pointer?

So let's say I have code similar to something like this.

#define DATA 0x67
char *mydata = malloc(1);
mydata = DATA

Line 3 is invalid, of course, but what I am looking of is to take 0x67 and read it into that memory location. It must be obvious, but I don't know what that would be called to ask the question properly.

Dereference the pointer is equivalent to access the first indice of an array (in this case an array of char) which is maybe more easily understandable:

#define DATA 0x67
char *mydata = malloc(1);
mydata[0] = DATA

If you would have allocated more memory you could access all bytes of this memory this way.

you need to 'derefernce' the pointer

#define DATA 0x67
char *mydata = malloc(1);
*mydata = DATA

think of it this way.

char *mydata

says that *mydata is a char, you can assign DATA to a char

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM