Reputation: 955
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
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
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