BEPP
BEPP

Reputation: 955

How to pass a array of strings to a function?

In the following program i just try to copy some string to the array and print it in another function.

I am getting segmentation fault .Could someone point out what i did wrong ?

  #include <stdio.h>
  #include <string.h>
  #define MAX_STR_LEN 20

  void print_str(char str[][],int count);

  int main()
  {
      char str[2][MAX_STR_LEN];
      strncpy(str[0],"hello",MAX_STR_LEN);
      strncpy(str[1],"world",MAX_STR_LEN);
      print_str(str,2);
      return 0;
  }
  void print_str(char str[][],int count)
  {
      int i;
      for(i=0;i<count;i++)
      printf("%s\n",str[i]);
  }

Upvotes: 0

Views: 55

Answers (3)

Chetan Raikwal
Chetan Raikwal

Reputation: 148

Provide the second dimension length in a 2-D array in C always. First dimension length is optional if you are declaring a 2-D array.

Upvotes: 1

msc
msc

Reputation: 34588

We need to specify the column size mandatory when passing a 2D array as a parameter.

So, You should declare your function like this:

void print_str(char str[][MAX_STR_LEN],int count);

Upvotes: 3

R Sahu
R Sahu

Reputation: 206577

Use

void print_str(char str[][MAX_STR_LEN], int count);

Upvotes: 1

Related Questions