Click to See Complete Forum and Search --> : C: array of strings passing as arguments problem
CaptainPinko
11-02-2003, 01:45 AM
i can get this working for one-dimensional arrays but for two dimensional arrays i get a [art@blackbox commerce]$ gcc arraypass.c
arraypass.c: In function `main':
arraypass.c:11: warning: passing arg 1 of `inc_array' from incompatible pointer type
arraypass.c: In function `inc_array':
arraypass.c:19: invalid use of array with unspecified bounds
arraypass.c:20: invalid use of array with unspecified bounds
error from GCC
#include <stdio.h>
void inc_array (char [][], int);
main () {
int test [3] = {1,2,3};
int ary [4] = {1,2,3,4};
int i;
char a [] [4] = {"cat", "dog"};
inc_array(&a,3);
printf ("%s %s\n", a[0], a[1]);
return 0;
}
void inc_array (char b[][], int size){
int i;
for (i = 0; i < size; i++){
b[0][i]++;
b[1][i]++;
}
}
i'm guessing that i'm missing something rather obvious about strings and pointers... any suggestions? i looked in my text book to but to no avail...
Citadel
11-02-2003, 02:35 AM
#include <stdio.h>
//void inc_array (char [][], int);
void inc_array (char [][4], int);
main () {
int test [3] = {1,2,3};
int ary [4] = {1,2,3,4};
int i;
char a [] [4] = {"cat", "dog"};
// inc_array(&a,3);
inc_array(a,3);
printf ("%s %s\n", a[0], a[1]);
return 0;
}
//void inc_array (char b[][], int size) {
void inc_array (char b[][4], int size){
int i;
for (i = 0; i < size; i++){
b[0][i]++;
b[1][i]++;
}
}
CaptainPinko
11-02-2003, 04:41 PM
does the '4' have to be hardcoded or can i pass it as a variable?
bwkaz
11-02-2003, 07:37 PM
It doesn't have to be hardcoded as a number per se, but it does have to be a constant expression.
The reason is that gcc needs to know it at compile time. The reason for that has to do with the way arrays are handled internally -- as pointers. When you have a multi-dimensional array, it's a set of pointers to pointers. gcc needs to know how many of these pointers to throw on the stack in a function call, so that it can generate correct code inside the function to take the pointers back off the stack. It also has to be able to generate correct code for "sizeof" constructs, both for each row and for the entire array.
If you can't make it a constant expression, then pass a pointer to a pointer instead. But make ABSOLUTELY sure that you don't overflow a buffer somewhere! Make sure the array ends in a NULL pointer, or pass the size along with it, or something like that. Because you will have to know how long the array is from inside the function.
CaptainPinko
11-02-2003, 07:40 PM
so multil-dimensional arrays need constant expressions OR pointer-arithmatic/pointer-to-pointer mess etc?
hmph, was this 'fixed' (i'm sure it has its benefits so i use the terms loosely) in C++?
mwinterberg
11-02-2003, 10:23 PM
You can also use VLA's (added in C99)...
#include <stdio.h>
void inc_array (int, int, char [][]);
main () {
char a[][4] = {"cat", "dog"};
inc_array(2, 4, a);
printf ("%s %s\n", a[0], a[1]);
return 0;
}
void inc_array (int row, int size, char b[row][size]){
int i;
for (i = 0; i < size - 1; i++) {
b[0][i]++;
b[1][i]++;
}
}
C++ didn't "fix" that issue internally. However, using the STL, you can create a multidimensional array using a vector of vector's or a valarray as outlined in TC++PL.
-Michael
CaptainPinko
11-02-2003, 10:26 PM
thanks mwinterberg and bkwaz, thanks bunches
mindcooler
11-02-2003, 10:28 PM
If I understand you correctly, you want to pass an array of strings to a function and increment each letter in those strings with one and you want to do this without having to hard-code the size of the array in the parameter list of your function. This can be done and here's one way:
#include <stdio.h>
void inc_array(char* b, size_t size);
int main()
{
char a[][4] =
{
"cat",
"dog"
};
inc_array(a[0], sizeof(a));
printf("%s %s\n", a[0], a[1]);
return 0;
}
void inc_array(char* b, size_t size)
{
size_t i;
for(i = 0; i < size; ++i)
{
if(b[i] != '\0')
{
++(b[i]);
}
}
}
Remember, that you cannot use sizeof to get the entire size of the string array if you allocate it dynamically or pass it to a function, then you will have to keep track of the size yourself.