LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Initializing a table of strings

I'm a user of CVI5.5+TS2.0.

I'm confused...
This will work:
char *p;
p = "Hello Wrold";

But this does not word";
char **p;
*p = "Hello World";

1) Can somebody explain this to me.

2) What solution can you offer me if my task is filling a table strings
(3 lines with 3 strings in each line). This has to be done NOT in the
decleration but in the program body. (reason is because I need to fill
the same table with different data few times).
The table is:
str11, str12, str13,
str21, str22, str33,
str31, str32, str33
The strings varies in length.
I began by delcaring char **Table, but then, any attempt to initialize the
tables (*Table = "ABC") gave m
e errors.

Thank you very much
0 Kudos
Message 1 of 2
(3,144 Views)
Hello,
First, if you want to have a two dimensional array of strings then you actually need a char ***, since you would have an array of arrays to arrays of characters (say that 10 times fast).
Second, you need to assign memory for each array of pointers that you use. So with your example, you'd need to allocate memory before doing the assignment.
So
char **p;
*p = "Hello World"
doesn't work, but
char **p;
p = malloc(sizeof(char*)*3);
*p = "Hello World";
or
char *p[3];
p = "Hello World";
does.
When doing this with a two dimensional array of strings your best bet would probably be to assign some or all of the memory statically (assuming that you will always have 3 lines with 3 strings in each line).
Here's a little example that asks the user to enter stri
ngs and then fills in a table and prints it out:

#include
int main()
{
char *p[3][3];
char buffer[100];
int i,j;
printf("Enter 9 strings:\n");
for(i = 0; i < 3; i++)
for(j = 0; j < 3; j++)
{
scanf("%s", buffer);
p[i][j] = malloc(sizeof(char)*(strlen(buffer)+1));
strcpy(p[i][j], buffer);
}
fflush(stdin);
printf("\nTable Display:\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
printf("%s\t", p[i][j]);
printf("\n");
}
printf("Hit q to quit.\n");
getchar();
return 0;
}

Note that this is all standard C programming, there isn't anything CVI specific here. You might want to find a C programming book.

Regards,
Ryan K.
NI
0 Kudos
Message 2 of 2
(3,144 Views)