0

I'm trying to assign a value to a char** variable. In my foo.h I've defined a couple of variables such as

#define APIOCTET         int
#define APILONG          long                  
#define APICHAR          char               
#define APISTRING        char* 

Now in my foo.cpp I'm tryng to use a method where

APILONG apiInitialize(APISTRING filePath, APISTRING* outputString)
{
  //open text file to where output will be printed
  //do other stuff, etc..
  //return result;
}

I'd like to assign a value to my APISTRING* outputString but I just can't figure out how to do so, I've tried many things which are basically a variation of the following code

APISTRING  error = "error";
APISTRING  other = "string";
APISTRING  charArr[] = { error, other, error };
APISTRING  *charArr2[] = { charArr };
errorString = *charArr2; 

Also im not 100% clear on what exactly is APISTRING* outputString. When I try to compile it gives me an error message where it mentions it's a char**. Is it a 2D array?.. A pointer to an array of chars?.. But most importantly, how would I assign a value for this variable? Thanks in advance.

David A.
  • 83
  • 1
  • 15
  • 3
    Why don't you use a `typedef`? – Piotr Siupa Aug 02 '15 at 19:11
  • 2
    Manipulating string characters is far more easier with std::string, think about it. – Richard Dally Aug 02 '15 at 19:12
  • 1
    `*outputString = "hello";` Is this what you are looking for? You never explained *what* it is you want to assign to `outputString`. – Igor Tandetnik Aug 02 '15 at 19:12
  • You ought to use const char* for string literals. Also, you could use the sized types int32_t instead. – Bathsheba Aug 02 '15 at 19:14
  • 2
    `char**` is a pointer to a pointer to a char, its not any sort of array or string. Your mistake is clearly that you think there is some language magic that will help you do this assignment. There isn't. If you want to assign an array you have to allocate the memory and copy the characters to the allocated memory. And you have to track and free the memory at the appropriate time. It's easier to use std::string which will behave a lot more like what you are expecting char* to behave. – john Aug 02 '15 at 19:15
  • 1
    Your question asks us how to achieve a particular language objective (assign a value to a variable of type `char **`) without explaining what result you are trying to achieve. But the answer is probably `*outputString = "error";`. – David Schwartz Aug 02 '15 at 19:18
  • Beyond all the above, ISO C++11 doesn't allow conversion from string literal to `char*`, so you have that to contend with as well. If your compiler isn't spewing warnings on things like `APISTRING error = "error";` you seriously need to jam up your warning levels. – WhozCraig Aug 02 '15 at 20:04
  • I can't really modify the code, it's part of a legacy code I've been given to work with. What I need to do is work with what I have and manipulate it to use in a different manner. Thanks for all your responses guys, very helpful! – David A. Aug 03 '15 at 20:46

2 Answers2

1

The APISTRING* is a pointer to a pointer to char. It holds an address which holds the address of the first character of the string in memory.

See this question for more info on double pointers in C/C++.

To assign to the string you would need to do *outputString = "string"

Community
  • 1
  • 1
James Pack
  • 824
  • 1
  • 10
  • 19
  • This is pretty much what I was looking for, also thank you for the other info on the double pointers! – David A. Aug 03 '15 at 20:44
0

APISTRING* outputString will be pre-processed and replcaed at compile-time as char** outputstring. Hence, outputString will be double pointer hence, you need to do it like this (below code). I combined both .h and cpp together for simplicity.

#include<iostream>
using namespace std;

#define APIOCTET         int
#define APILONG          long                  
#define APICHAR          char               
#define APISTRING        char* 

APILONG apiInitialize(APISTRING filePath, APISTRING* outputString)
{
    APISTRING getIt = *outputString;
    cout<<"  "<<getIt<<endl;
}

int main()
{
    APISTRING str = "hello";
    APISTRING* outputString = &str;
    APILONG val = apiInitialize("world", outputString );

    system("PAUSE");
    return 0;
}

I would recommend to use std::string, it'll be easy to tweak with certain behaviors. Hope this helps.

instance
  • 1,366
  • 15
  • 23