0

I have the following :

char *testString[] = {"geeksforgeeks", "unsorted", "sunday", "just", "sss" , NULL};

I need to traverse this so I am using:

 char **a1 = testString;

So I need to understand what is happening here now.

What I understand :

  • a1 would hold the address of first index of testString.

  • *a1 = value at address of the first string.

< This is incorrect. *a = the first string on IDE>

  • **a1 = should be the first string.

  • *testString = the first string..

Where and why am I going wrong?

9
  • 4
    In C++, the string type is spelled std::string. Are you sure you are writing in C++ and not in some other language that has the capital C in its name? Commented Sep 19, 2015 at 17:10
  • @n.m.: Cobol? C#? Objective-C? :) Commented Sep 19, 2015 at 17:12
  • trust me .. you can run this on cpp IDE and it will work .. Commented Sep 19, 2015 at 17:12
  • @BhumiSinghal I don't trust anything than a minimal reproducible example that's shown in your question. As is, it's off-topic, period! Commented Sep 19, 2015 at 17:14
  • The name of languagge i am sure does not matter to people .. who know the answer .. so since you know the name of the language so well .. may be the implementation explaination as well too ? Commented Sep 19, 2015 at 17:14

1 Answer 1

1

In your code testString is an array of pointers to char which you can modify (eg. testString[0]="test"). While a1 is a pointer to pointer to char, which you cannot modify (you can make it point to something else but you can't modify the strings).

So, **a1 holds the whole array, so *a1 is the first string.

Moreover, you could have written char **testString={"one string", "another string"} if you don't need to change testString.

Here you are a live example.

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

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.