Hello, I am working with variable arguments and I have no problem using them with Integer, Float or char arrays. But when I try to use String arrays, it is a multidimensional array and I am not sure ...
I am calling a function with variable arguments, one of which could be a string array. And the string array gets populated in the function. I get the right pointer to the first index, I can change its value properly inside the function but I cannot get to the second index, nor do I see the right pointer address for the second index. I think it has something to do with allocating right size memory but I'm a little confused.
Here is my test code -
int fetchroutStr(int numArgs, ...)
{
va_list arg_ptr;
int counter = 0;
int numParam = 0;
char *tempStr[100];
int args = 0;
va_start(arg_ptr, numArgs);
numParam = numArgs;
while(counter < numArgs)
{
tempStr[0] = va_arg(arg_ptr, char **);
printf("\nFetch Pointer : 0x%p", tempStr[0]);
printf("\nFetch Loop : %d - %s", counter, tempStr[0]);
// printf("\nPointer 0x%p", &tempStr[1]);
// printf("\nIn loop: %d - %s", counter, tempStr[1]);
counter++;
}
va_end(arg_ptr);
strcpy(tempStr[0], "funcStr1");
printf("\nFetch Field changed: %s", tempStr[0]);
// strcpy(tempStr[1], "funcStr2");
// printf("\nfetch 2: %s", tempStr[1]);
}
void main() {
char testStr [10][20];
strcpy(testStr[0], "strMain1");
strcpy(testStr[1], "strMain2");
printf("\nMain Field 1: %s",testStr[0]);
printf("\nMain Field 2: %s",testStr[1]);
printf("\nMain Pointer 1: 0x%p", &testStr[0]);
printf("\nMain Pointer 2: 0x%p\n\n", &testStr[1]);
fetchroutStr(1, testStr);
printf("\n\n\nMain After function Field 1: %s", testStr[0]);
printf("\nMain After function Field 2: %s \n\n", testStr[1]);
}