LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to access the values from readonly array of strings (https://www.linuxquestions.org/questions/programming-9/how-to-access-the-values-from-readonly-array-of-strings-4175454638/)

kelvincholamattathil 03-19-2013 02:47 AM

how to access the values from readonly array of strings
 
I need to access newPrompt1 values .But its showing NULL ,is there any way to get the values

#include<stdio.h>
#include<string.h>
int main()
{
unsigned char *newPrompt="# ";
unsigned char **newPrompt1 =NULL;
unsigned char au1CLIPromptStrings [7][30] =
{
"",
"Login: ",
"Password: ",
" 0123456789012345678901234",
" 0123456789012345678901234",
};
newPrompt1 = au1CLIPromptStrings; // here am assigning the address
printf("b1 = %u and b2 = %u\n",newPrompt1,au1CLIPromptStrings);
printf("a1 = %s and a2 = %s\n",newPrompt1[3],newPrompt1[4]);
}

pan64 03-19-2013 03:20 AM

it is set to NULL, so it must show null. Am I missed something?
sorry, in your code you have 2 { and 3 }, so it is not ok. What have you really made?

knobby67 03-19-2013 03:39 AM

Are you using c or c++? I think c? strcpy might be useful for you.

mina86 03-19-2013 08:40 AM

au1CLIPromptStrings is a two dimensional array so it's not compatible with unsigned char **. What you need is to define it as an array of pointers to const char. Also note, that you should not use unsigned char in this context.

Code:

const char *arr[] = { "foo", "bar", "baz" };
const char **str = arr;
size_t i;

for (i = 0; i < sizeof arr / sizeof *arr; ++i, ++str) {
    puts(*str);
}



All times are GMT -5. The time now is 03:52 AM.