Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing array of character strings to function

Tags:

c

I am trying to pass an array of character strings (C style strings) to a function. However, I don't want to place a maximum size on length of each string coming into the function, nor do I want to allocate the arrays dynamically. Here is the code I wrote first:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void fun(char *s[])
{
    printf("Entering Fun\n");
    printf("s[1]=%s\n",(char *)s[1]);
}

int main(void)
{
    char myStrings[2][12];

    strcpy(myStrings[0],"7/2/2010");
    strcpy(myStrings[1],"hello");
    fun(myStrings);

    return(0);
}

I got a seg fault when run and the following warning from the compiler: stackov.c: In function ‘main’: stackov.c:17: warning: passing argument 1 of ‘fun’ from incompatible pointer type stackov.c:5: note: expected ‘char **’ but argument is of type ‘char (*)[12]’

However, when I change the main() to the following it works:

int main(void)
{
    char myStrings[2][12];
    char *newStrings[2];

    strcpy(myStrings[0],"7/2/2010");
    strcpy(myStrings[1],"hello");
    newStrings[0]=myStrings[0];
    newStrings[1]=myStrings[1];
    fun(newStrings);

    return(0);
}

Isn't array[2][12] the same thing as an array of character pointers when it is passed to a function?

like image 467
Mike Avatar asked Aug 18 '10 15:08

Mike


People also ask

How do you pass a char array to a function?

A whole array cannot be passed as an argument to a function in C++. You can, however, pass a pointer to an array without an index by specifying the array's name. In C, when we pass an array to a function say fun(), it is always treated as a pointer by fun().

How do you pass a character array to a function in Java?

Method 1: Using String Class Constructor Arrays of characters can be passed to the constructor of the String class, which allocates these characters into the new String. Using the constructor of this class, you will get the sequence of characters, i.e. a string from a char[] array. Here value is the character array.

How do you pass a string to a function?

Passing one dimensional string to a function To pass a one dimensional string to a function as an argument we just write the name of the string array variable. In the following example we have a string array variable message and it is passed to the displayString function.


2 Answers

No, char array[2][12] is a two-dimensional array (array of arrays). char *array[2] is an array of pointers.

char array[2][12] looks like:

7/2/2010\0\x\x\xhello\0\x\x\x\x\x\x

where \0 is NUL and \x is indeterminate.

while

char *array[2] is:

0x CAFEBABEDEADBEEF

(assuming 32-bit)

The first has 24 contiguous characters, the second has two pointers (to the beginnings of strings elsewhere).

like image 111
Matthew Flaschen Avatar answered Nov 13 '22 10:11

Matthew Flaschen


Try

void fun(char s[][12]) { ...}

Read also from the c-faq: My compiler complained when I passed a two-dimensional array to a function expecting a pointer to a pointer

like image 22
Tom Avatar answered Nov 13 '22 10:11

Tom