2

Could anybody please tell me why is the output of both the prints are not same? How to pass an array of strings to a function then?

main()
{
    char b[10][10];
    printf("%p, %p, %p\n", b, b[0], b[9]);
    getStrListFromString(b);
}

void getStrListFromString(char **strList)
{
    printf("%p, %p, %p\n", strList, strList[0], strList[9]);
}

Expected OUTPUT :

0x7fffbe4ecf00, 0x7fffbe4ecf00, 0x7fffbe4ecf5a
0x7fffbe4ecf00, 0x7fffbe4ecf00, 0x7fffbe4ecf5a

Actual Output :

0x7fffbe4ecf00, 0x7fffbe4ecf00, 0x7fffbe4ecf5a
0x7fffbe4ecf00, 0x7fffbe4ecf80, (nil)
4
  • 1
    that's not the way to pass 2d array Commented Mar 22, 2017 at 12:34
  • try void getStrListFromString(char strList[10][10]) for the prototype. 2D arrays are not pointers on 1D arrays. Commented Mar 22, 2017 at 12:35
  • Also, pointers printed using %p must be cast to void *. Commented Mar 22, 2017 at 12:39
  • Stop wasting your time and enable compiler warnings/errors. This code has 3-4 of them that shouldn't be ignored. Commented Mar 22, 2017 at 12:42

2 Answers 2

3

Your function expects a char ** but you're passing it a char [10][10]. These are not the same.

An array when passed to a function decayed to a pointer to the first element. So when you pass a char [10][10] (an array of size 10 of char [10]) to a function it decays into a char (*)[10] (a pointer to a char [10].

Change your function to accept either char [10][10] or char (*)[10].

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

4 Comments

I understood. But couldn't understand why I am getting b[0] as 0x7fffbe4ecf00 in main but strList[0] as 0x7fffbe4ecf80 in the function.
@Karthik Undefined behavior. When you pass an incompatible pointer to a function, you can't accurately predict what will happen.
but there is certainly a predictable pattern. I always get (address + 128 bytes)?
@Karthik You can't depend on that behavior. For example, when I run this code I get address + 70.
2

In addition to the core issue about pointer-to-pointer having absolutely nothing to do with multi-dimensional arrays, your also has various other bugs:

main() // obsolete form of main(), won't compile unless C90
{
    char b[10][10]; // uninitialized
    printf("%p, %p, %p\n", b, b[0], b[9]); // should be void pointers
    getStrListFromString(b); // wrong type of parameter
    // return statement necessary here in C90
}

void getStrListFromString(char **strList) // should not be pointer-to-pointer
{
    printf("%p, %p, %p\n", strList, strList[0], strList[9]); // same issue as in main
}

The fact that this code compiled means that your compiler is either complete crap or incorrectly configured. You need to fix that asap.

Corrected code:

#include <stdio.h>

void getStrListFromString(char strList[10][10])
{
    printf("%p, %p, %p\n", (void*)strList, (void*)strList[0], (void*)strList[9]);
}

int main (void)
{
    char b[10][10];
    printf("%p, %p, %p\n", (void*)b, (void*)b[0], (void*)b[9]);
    getStrListFromString(b);
}

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.