-1

I'm trying to retrieve some text from a char array, like this:

unsigned char some_variable[3][10] = {"text1","text2","text3"};

int i;

for(i=0;i<3;i++){
    functionIcantChange(some_variable[i])
}

I'm getting this issue since this functionICantChange takes aguments like "hello" and "world" with " characters included. And by calling for example some_variable[0] I just get text1 for example.

I need to add the " character to the array before passing the array to the function. How could I do this?

Ariel
  • 1,059
  • 2
  • 13
  • 22
  • This should be `char* some_variable[] = { ... }`. – tadman Nov 10 '20 at 23:57
  • 2
    If you want quotes in your string: `"Say \"hello\""`. – tadman Nov 10 '20 at 23:58
  • Recursively? Where's the recursion? – jarmod Nov 10 '20 at 23:58
  • I need something like unsigned char variable[1][10]={""text1""} so variable[0] returns "text1" instead of text1 – Ariel Nov 10 '20 at 23:58
  • By the way, you should use `char` to represent characters, not `unsigned char`. – jarmod Nov 11 '20 at 00:00
  • @Ariel my answer below does that... Not sure if that's what you mean, -- Also recursive is a bit confusing. – Ilan Keshet Nov 11 '20 at 00:01
  • @jarmod *By the way, you should use `char` to represent characters, not `unsigned char`* Yes, it's OT, but can you explain why? – Andrew Henle Nov 11 '20 at 00:05
  • 1
    @AndrewHenle good discussion here: https://stackoverflow.com/questions/75191/what-is-an-unsigned-char. Additionally here: https://wiki.sei.cmu.edu/confluence/display/c/STR04-C.+Use+plain+char+for+characters+in+the+basic+character+set – jarmod Nov 11 '20 at 00:10
  • @IlanKeshet It was my fault, this snippet doesn't use anything recursively. I removed it from the question. And yeah, your answer is what I wanted to, thanks – Ariel Nov 11 '20 at 00:14

2 Answers2

1

I recommend you use sprintf to insert quotations in the front and back of your array. You would need some large buffer to hold the value of the variable.

char buffer[255];
sprintf(buffer, "\"%s\"", some_variable[i]);
functionIcantChange(buffer);
Ilan Keshet
  • 514
  • 5
  • 19
0

I need something like unsigned char variable[1][10]={""text1""} so variable[0] returns "text1" instead of text1

Escape the quotes.

char some_variable[3][10] = {"\"text1\"","\"text2\"","\"text3\""};

See cppreference escape sequences.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111