-1

I have two variables as:

char** A;
std::string B;

An assignment like this happens somewhere in the code:

B = A[0];

How should I give the value to A so that the assignment happens properly.

I tried this:

char* A_ = NULL;
A_ = (char*)"abcd";
A = &A_

When the assignment happens later, I get the error like this:

Invalid read of size 8.

Please help to solve this issue. Thanks.

user3552519
  • 401
  • 4
  • 11
  • 1
    Casting to force something into happening is the wrong solution 98% of the time. A [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) should teach that. – StoryTeller - Unslander Monica Mar 07 '18 at 06:41
  • @StoryTeller Do u have a solution for my problem. – user3552519 Mar 07 '18 at 06:45
  • 2
    Of course, but I was hoping you'd accept a book about fishing before I hand you a fish for the day only. – StoryTeller - Unslander Monica Mar 07 '18 at 06:47
  • @user3552519 It sounds very dubious, but if you want this to happen then I think you need some more variables `char tmp1[] = "abcd"; char* tmp2 = tmp1; char** A = &tmp2;` But clearly something is wrong with your code if you have to write stuff like this. – john Mar 07 '18 at 06:49
  • `char S[] = "abcd", *P = S, **A= &P;` That's it. I think you need to spend more time understanding pointers, then pointers-to-pointers and how they work. Fyi: hard casts are nearly always a horrifying code smell, so if you find yourself doing them, something is probably *very* wrong. – WhozCraig Mar 07 '18 at 06:53
  • There isn't really enough information here to give a good answer. Is `A` supposed to point to an array of strings (presumably yes)? What sort of lifetime does the array pointed to by `A` need to have? What sort of lifetime do the strings in the array pointed to by `A` need to have? When you say "happens somewhere in the code", where? In the same function? – Miles Budnek Mar 07 '18 at 07:00
  • @MilesBudnek The char** variable is passed via reference to a function and later when the function ends, the variable is assigned to a string variable. – user3552519 Mar 07 '18 at 07:05

3 Answers3

1

There is no single assignment of A possible that meets your requirements, since there are two assignments necessary;

  1. Initialise A so its value is the address of a valid char *
  2. Initialise that char * to point to the (first character of) an array of char that contains a nul terminated string.

For example, assuming B is a static

std::string B;

void some_func()
{
    char some_thing[] = "abcdefg";

    char *p = some_thing;       // same effect as A[0] = some_thing or as *a = some_thing

    char **A = &p;

    B = A[0];    // note the assignment COPIES some_thing

    //   p and some_thing both cease to exist as function returns
    //     that is OK as B contains a copy of some_string
}

Note that I've carefully constructed this to work, as per your requirement. However, what you are trying to do is error prone (e.g. very easy to misuse any of the pointers in a way that causes undefined behaviour) so is generally considered poor practice in C++.

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Peter
  • 35,646
  • 4
  • 32
  • 74
0

i think direct access to char** to A[0] is not possible cause its not a 2 dimensional, below things you can try

 char **str = NULL;
    char *st = NULL;
    st = "Hello";
    str = &st;
    printf(" %s ", *str);

    st = "World";

    printf(" %s ", *str);
Sachin Nale
  • 139
  • 2
  • 4
-1

Use a single pointer . Double pointer are analogous to 2d arrays.

char* A;
std::string B;
A = (char*)&("abcd");

B  = A;
kelvin
  • 1
  • 1