9

I am programming in C using Atmel Studio (for those unfamiliar with this it is used to program to micro controllers such as arduino. You can not simply print, you have to send the data Serially to an application such as Terminal.)

I have so far:

uint8_t * stackHolder;
char buffer[128];
stackHolder = &buffer[127];

Lets say that the address of buffer[127] happens to be 0x207. I can see that the value of stackHolder is 0x207 using the debugger. I have a function that takes a char * and prints that. So my question is how can I convert the uint8_t * stackHolder into a char * so I can pass it to my print function?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
skyleguy
  • 979
  • 3
  • 19
  • 35
  • C does not support _methods_. Sure you don't use C++? And why do you want to convert a pointer? – too honest for this site Mar 22 '17 at 19:56
  • 2
    Converting between types is what casting is for. – John Bollinger Mar 22 '17 at 19:56
  • 1
    Perhaps you need to convert those signed and unsigned `char` types to `int` to get the full range. – Weather Vane Mar 22 '17 at 19:59
  • What is in your buffer? ASCII-characters? If so, eliminate the uint8_t pointer and just use a char pointer for stackHolder. – Andrew Cottrell Mar 22 '17 at 20:13
  • to clarify, I am tasked with writing a program that moves the location of the stack Pointer (for context switches I believe). the stack pointer in my microcontroller consists of 2 uint8_t registers. I am simply trying to: a: get the address it is pointing to and printing it, and b: printing what is actually stored at that address. Whenever I cast it does not print anything so I assumed that that was not working correctly. I assume that my buffer is filled with 0's because I think that is how C initializes them if you dont give a value – skyleguy Mar 22 '17 at 20:18
  • FYI, "I assume that my buffer is filled with 0's because I think that is how C initializes them if you dont give a value"... no, it doesn't, you will get garbage values. – SGeorgiades Mar 18 '22 at 17:41
  • If a cast doesn;t fix the problem, post more complete code so we can hekp you troubleshoot. – SGeorgiades Mar 18 '22 at 17:43

2 Answers2

21

How can I convert the uint8_t * stackHolder into a char *?

By casting:

print((char*) stackHolder);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
6

I have a method that takes a char *

Suggest minimizing casts to one location to accomplish the goal and retain function signature type checking. Create a wrapper function with a cast.

// OP's original function
extern void skyleguy_Print(char *);

void skyleguy_Print_u8(uint8_t *s) {
  skyleguy_Print((char *) s);
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256