1

How to retrieve the data from domain_cx in domaine , once I'm in the function get_domaine(...) I have tried the following, but the result isnt what expected (0-2 instead of 0-2 3). Here is my code

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

int get_domaine(char * domaine)
{
  char (*ptr_dom)[10];
  ptr_dom = (char (*)[10]) domaine;
  printf("%s ", ptr_dom[0]); // will print
  printf("%s ", ptr_dom[1]); // will not print even thought there is data.
  return 1;
}

int main(int argc,char * argv[])
{
   char *domaine_cx[10];

   domaine_cx[0] = "0-2";
   domaine_cx[1] = "3";

  if(get_domaine((char *)*domaine_cx)) printf("Ok");
  return EXIT_SUCCESS;
}
1
  • I edited it, it does compile now. Commented Oct 20, 2013 at 23:22

1 Answer 1

5

You are trying to cast char (*) [10] to char * just so that you can try to cast it back afterwards. Much simpler would be changing the prototype of your function instead:

int get_domaine(char* domaine[10])
{
    printf("%s ", domaine[0]);
    printf("%s ", domaine[1]);
    return 1;
}

and in main:

if (get_domaine(domaine_cx))
    printf("Ok");

Alternatively you might not pass an array of any size and pass the size in other argument explicitly:

int get_domaine(char* domaine[], int tokens)
{
    int i;
    for (i = 0; i < tokens; ++i)
        printf("%s ", domaine[i]);
    return 1;
}

and in main:

if (get_domaine(domaine_cx, 2))
    printf("Ok");

And in case you really can not change the prototype, than this evil cast would do:

int get_domaine(char* domaine)
{
    char** myEvilPtr = (char**) domaine;       // !!! we don't know the size !!!
    printf("%s ", myEvilPtr[0]);
    printf("%s ", myEvilPtr[1]);
    return 1;
}

and since arrays decay into pointers, in main you could still do simple:

if (get_domaine(domaine_cx))
    printf("Ok");
Sign up to request clarification or add additional context in comments.

10 Comments

try function declaration int get_domaine(char* domaine[]), or get_domaine(char** domaine)
I have no control over the signature, unfortunately
Since i know the size of the array of char* I can cast it to a pointer to a char (the first char) , knowing the size , can't I use it from that pointer to a char ?
@user2827260: As far as I'm aware, there are no guarantees that a char * will be the same size as a char **, although on most systems you'll encounter that will be the case. As such, anything you can do will probably be undefined behavior.
@elgonzo: You can't cast pointers around willy-nilly. You can cast a pointer to something to a pointer to char, a pointer to void, back to whatever it originally was pointing to, and, as far as I know, nothing else without triggering undefined behavior. Furthermore, as far as I know, the standard gives no guarantees to sizes of pointers beyond that a void * is big enough to hold any other pointer.
|

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.