简体   繁体   中英

C : uninitialized char pointer and print out the address

I have some local pointers and before which I am given two global pointers.

My task is to print out every byte between the two global pointers +/- 32. And the local pointers will be different because I will mark it different than global pointers.

And my question is how to initialize at first every byte between the two pointers? The following is my try but not working:

// global scope
char * minimum_ptr, * maximum_ptr;

int main()
// inside main function
char * temp_ptr = minimum_ptr-32;
char * add_ptr = minimum_ptr-32;
while (temp_ptr != maximum_ptr+32) {
  add_ptr = NULL;
  temp_ptr++;
}

// getting errors
printf("%p", (char *)(minimum_ptr));
printf("%p", (char *)(minimum_ptr+1));

I corrected it, but still confusing sorry. I am confused too. All I want, is to print out the address between the two pointers, minimum_ptr-32 and maximum_ptr+32, and minimum_ptr and maximum_ptr are not initialized yet but I might need so if I need to.

Addition: I want to initialize all of them first and do some assignment after then so that I can print out everything between and see what and where has been assigned. Thanks

First of all you need to be sure that the pointers are showing the correct addresses. Pointers are not different then arrays (yes they are different but..), so you may assume them to be arrays. I assume that the maximum_ptr's address is greater than the minimum_ptr's address.

uint dist = maximum_ptr - minimum_ptr;
for (int i = 0; i < dist; ++i) {
   minimum_ptr[i] = 'A';
}

or you might use std::fill

std::fill(minimum_ptr, maximum_ptr, 'a');

http://www.cplusplus.com/reference/algorithm/fill/

you might adjust the pointer values

std::fill(minimum_ptr - 32, maximum_ptr + 32, 'a');

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