0

Hello How to print string which is passed from 2d array to function called print_2d and in that function i am catching base address of 2d array which has string as its element

now with pointer to an array now i want to print the string using pointer to an array? my code is below

#include<stdio.h>
void print_2d(char (*p)[10],int r) // i am catching with pointer to an array
{
    int i;
    for(i=0;i<r;i++)
    printf("%s",*p[i]); //segmentation fault why?
                            // how to print then?
}

main()
{
    char *s[10]={"one","two","three","four"};
    int r;
    r=sizeof(s)/sizeof(s[0]);
    print_2d(s,r);
}
4
  • Do you want a pointer to an array or an array of pointers? Commented Aug 16, 2014 at 3:40
  • @Code-Apprentice pointer to an array (*p)[10]? Commented Aug 16, 2014 at 4:47
  • 2
    But s is declared as an array of pointers... Commented Aug 16, 2014 at 4:55
  • Note that there is no 2D array present in the code shown. There is an array of pointers, but that's a 1D array. Commented Aug 17, 2014 at 7:17

4 Answers 4

2

Your print_2d function is declared as accepting a pointer to char[10] array. However, nowhere in your code there's a char[10] array. There's noting in your code that can be legally passed to print_2d.

Anyway, it is not possible to "fix" your code, until you explain what you are trying to do.

Judging by the body of print_2d and by what you have in main, you might need the following

void print_2d(char *(*p)[10],int r)
{
  int i;
  for (i = 0; i < r; i++)
    printf("%s", (*p)[i]);
}

int main()
{
    char *s[10] = { "one", "two", "three", "four" };
    int r;
    r = sizeof(s) / sizeof(s[0]);
    print_2d(&s, r);
}

Note that parameter p is declared as a pointer to an array of char * pointers, which is what your s in main is. However, you have to realize that pointer to array can only point to an array of fixed pre-determined size. The array passed to print_2d will always have size 10 and only 10. For this reason, it makes no sense to pass array size to print_2d separately. Just calculate the size inside and get rid of the second parameter

void print_2d(char *(*p)[10])
{
  int i, r;
  r = sizeof *p / sizeof **p;
  for (i = 0; i < r; i++)
    printf("%s", (*p)[i]);
}

int main()
{
    char *s[10] = { "one", "two", "three", "four" };
    print_2d(&s);
}

Alternatively, your code can be fixed as shown in @BLUEPIXY's answer, but that is a completely different way to interpret your intent. And we don't know what your intent was.

Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot from last few days i was search lot for the solution how to catch array of pointer with pointer to an array..finally i got thanks a lot :-)
2

Try this

#include<stdio.h>
void print_2d(char *p[10],int r) // This is another change.
{
    int i;
    for(i=0;i<r;i++)
        printf("%s",p[i]); // This is the main change
}

int main() // main has to return int
{
    char *s[10]={"one","two","three","four"};
    int r;
    r=sizeof(s)/sizeof(s[0]);
    print_2d(s,r)
}

s[0], s[1] etc are the strings. Why are you trying to print *s[0], *s[1] etc?

4 Comments

sorry char*p[10] is an array of pointer actually i want to catch with pointer to an array (*p)[10] and then print them?
@SumitKumar The "main change" is the important thing here. Because of the implicit conversion when passing a parameter, the difference between an array of pointers and a pointer to an array is probably irrelevant. (However, there might be other implications that aren't being addressed by this question.)
@SumitKumar - from main you are passing an array of pointers. Hence you should accept an array of pointers & not pointer to an array.
@user93353 that was only my question i was asked in an interview i have to catch array of pointer with pointer to an array?please can u explain is it possible i said him its not possible
1

The problem you are having is r = 10 every time. So when you pass print_2d(s,r) you are telling print_2d there are 10 strings to print. You cannot get the r using sizeof (arr)/sizeof(arr[0]) it will return 10 each time. You have created a pointer to 10 character arrays with your assignment and initialization (which null terminates the 4 given strings by default and leaving 6 empty). To prevent teh segfault, you need to pass the number of character arrays to print_2d. For example:

#include<stdio.h>

void print_2d(char **p, int r) // i am catching with pointer to an array
{
    int i;
    for (i=0; i<r; i++)
        printf ("%s\n", p[i]);
}

int main (void)
{
    char *s[10] = {"one","two","three","four"};
    int r;
    // r = sizeof(s)/sizeof(s[0]);  // r = 10 causing segfault @ i = 4 in print_2d
    // you cannot get the number of string arrays in this manner. 
    r = 4;
    printf("r: %d\n", r);
    print_2d (s,r);
    return 0;
}

output:

$ ./bin/par
r: 4
one
two
three
four

If you want to use sizeof (s) / sizeof (s[0]) then you will need to declare s as follows:

char *s[] = {"one","two","three","four"};
int r;
r = sizeof(s)/sizeof(s[0]);  // Now r will equal 4

2 Comments

Though, it's better to not declare size of array in this case - declaring it shouldn't cause an issue - s[4] to s[9] would be null pointers.
No not in this case. Take a 2D array s[row][col]. When your convert to pointer notation *(s[row] + col) as done here, you are creating a pointer to 10 strings with *s[10]. While 4-9 may be empty sizeof(s)/sizeof(s[0]) will still return 10 instead of 4. By declaring *s[] you let the compiler fill in the number of rows from the designation of string literals. (thank you, you helped catch a mistake in my answer)
1

char (*p)[10] is pointer to char[10], not char *s[10] (array of 10's pointer to char)
There is a need to change the main and if void print_2d(char (*p)[10], int r) is correct.

#include <stdio.h>

void print_2d(char (*p)[10], int r){
    int i;
    for(i=0;i<r;i++)
        printf("%s\n", p[i]);
}

int main(){
    char s[][10]={"one","two","three","four"};
    int r;
    r=sizeof(s)/sizeof(s[0]);
    print_2d(s,r);
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.