开发者

C++: How do I dereference multiple characters from a character pointer in memory using the namelength

开发者 https://www.devze.com 2023-03-01 00:22 出处:网络
Here is the code: errorLog.OutputSuccess(\"Filename reference: %c\", *t_current_node->filename); It of course only outputs the first character. If I add something like ->filename[nameLen] whe

Here is the code:

errorLog.OutputSuccess("Filename reference: %c", *t_current_node->filename);

It of course only outputs the first character. If I add something like ->filename[nameLen] where nameLen is a valid integer of say 10 it says:

开发者_Python百科

operand of * must be a pointer.

Thanks!


If the string is terminated with \0, you could use %s instead:

errorLog.OutputSuccess("Filename reference: %s", t_current_node->filename);

You also will need to pass the memory address of filename, so lose the *symbol.


Use %s, and remove the *

 errorLog.OutputSuccess("Filename reference: %s", t_current_node->filename);


  • %c prints a single character.
  • %s prints a string: all characters up to the terminating \0.
  • %.10s prints the first 10 characters of a string (or less, if the string is shorter)
  • %.*s takes two arguments, an integer indicating the length to print, and a string pointer.

Example for the last case:

printf("Filename reference: %.*s", nameLen, t_current_node->filename);

0

精彩评论

暂无评论...
验证码 换一张
取 消