21
Dec
char** from static variables
Related Blog Items
- What is a reentrant function?
- Unions in C++
- C/C++ Questions asked in various job interviews
- Heap Overview
- Different signature problem with extern variables
When a function expects a char** parameters, it is not possible to pass a char[][] variable, which should have allowed us to avoid dynamic memory allocation. Merely casting the char[][] will produce undesirable results. Example:
void iterate(char** a, count) {
for(int i=0; i<count; i="">
operate(a[i]);
}
}
int main () {
char str_array[32][128];
iterate((char**)(str_array), 32);
return 0;
}
The a[i] will reference (char*)str_array+sizeof(int), a lot different from what we want str_array[i].
However, we can do this:
int main() {
char buffer[32*128];
char* str_array[32];
for(int i=0; i< ++i) {
str_array[i] = buffer+i*128;
}
iterate((char**)str_array, 32);
return 0;
}
This will produce the desired effect
Popularity: 5%
You need to log on to convert this article into PDF
Related Blog Items - What is a reentrant function?
- Unions in C++
- C/C++ Questions asked in various job interviews
- Heap Overview
- Different signature problem with extern variables
Related Blog Items
- What is a reentrant function?
- Unions in C++
- C/C++ Questions asked in various job interviews
- Heap Overview
- Different signature problem with extern variables
Why not just
char a[200][200];
char**b=a;
iterate(b,NUM);
May 31st, 2008 at 4:17 pm