简体   繁体   中英

C array 64 bit increment

I have:

uint64_t *list;

if((list = malloc(10 * sizeof(uint64_t))) == NULL){
    errx(1, "malloc");
} 

I need to populate this array, increment the address of it.

I mean:

(*list)++;
list = 1;
(*list)++;
list = 2;
(*list)++;
list = 3;
(*list)++;
list = 4;

How do I do that?

You can use the allocated memory like a normal array:

list[0] = 1;
list[1] = 2;
/* etc. */

Edit: What you are doing is increase the first entry in the "array" which probably contains a value you don't expect, then you reassign the pointer so it no longer points to your allocated memory, and so on. Also, if you really want to "increment the address" of the allocated memory, it's just simple as list++ , however this is also changing the pointer so you loose the original allocated address and can not free it later (unless you save it.)

you can use a loop

for(int i = 0; i < 10; i++) {
    list[i] = i+1;
}

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