-1

I'm really new to C pointers and I'm coming from Java/C++. I am trying to append a char* c to a char** cArray. Here is kind of what I have so far:

char **cArray = "abc";
char *c = "def";
cArray += &c;
printf("%s", cArray)

and output should be: abcdef

My question is, How do I append a char * to a char ** in C? Thank you for any tips you have!

Greg
  • 305
  • 1
  • 4
  • 11
  • And the question is? – axiac Apr 09 '16 at 20:58
  • How to append a char* to char** in C – Greg Apr 09 '16 at 21:01
  • 1
    I think you are looking to copy the string to the end of string. You may want to look at string functions in C. – randominstanceOfLivingThing Apr 09 '16 at 21:01
  • 2
    Do you know what a `char*` and a `char**` are? Appending one to the other doesn't make much sense. – juanchopanza Apr 09 '16 at 21:02
  • I thought char* is a string basically, and char** an array of strings. Im trying to add the string to the end of the string array. And im trying to learn pointers so I'm doing it this way, trying to avoid string libraries if possible – Greg Apr 09 '16 at 21:04
  • 1
    See the `strcat()` function. Any C textbook or tutorial should explain how to work with strings and pointers. – Barmar Apr 09 '16 at 21:06
  • 1
    A `char*` is a pointer to `char`. A `char**` is a pointer to a pointer to `char`. A pointer to `char` can point to the beginning of a null terminated string, but it doesn't have to. It can also point to the beginning of an array of `char`. Or just to a single `char`. From there, you can figure out what a `char**` can do. – juanchopanza Apr 09 '16 at 21:06
  • 1
    You have to use the string functions, C doesn't have built-in operators that process strings. – Barmar Apr 09 '16 at 21:06
  • So i cant use char* to mimic a string? I've seen http://stackoverflow.com/questions/5580761/why-use-double-pointer-or-why-use-pointers-to-pointers – Greg Apr 09 '16 at 21:09
  • I want to add the values of char* to the end of the values in char** without using string libraries – Greg Apr 09 '16 at 21:16
  • @Greg You have a subtle contradiction in your requirements. On the one hand, you're talking about making an *"array of strings"*. But on the other hand, the `printf` treats `cArray` as if it was a single string, and the expected output appears to be a single string. So which is it, an array of strings `char *array[] = { "abc", "def" }`, or a single string made by concatenating two smaller strings? – user3386109 Apr 09 '16 at 21:21
  • It's suppose to be a char** segment in which I append the values from a char* to the end – Greg Apr 09 '16 at 21:29

1 Answers1

4

You seem to have a misunderstanding of what a pointer and an array are. First lets start with an array. An array is a contiguous fixed-size block of memory. That is to say, an array is a constant number of values next to each other.

So, to start with, the idea of "Appending" an array makes sense in the way that you can add an item to the next empty spot in an array. But it would not be right to say the array is growing. An array is not the equivalent of Java's Array List.

Lastly, I will point out static arrays are declared with:

int val[3];

Where 3 can be any other constant value interpreted as a size_t.

Next, lets discuss pointers. Pointers are not arrays, do not confuse the two- although many people find it comforting to think of them as interchangeable (and for the most part you can get away with it!). However, this is one of the cases where they are not. So what are pointers?

Pointers denote the location of a value in memory. So, a pointer could say point to an element in our val array we created above. If we created a pointer to point at each element in our val array and we printed them all to stdout we would see that they are all 4 bytes away from each other. This is because arrays have their values located right next to each other (contiguous in memory) and sizeof(int) would return 4 (on my system).

Your main misunderstanding seems to be that pointers need to point to anything at all. They do not. Just like you can have a value which holds no information (or all of the bits are set to 0), you can surely have a pointer that points no nowhere at all. As a matter of fact that's exactly what you do.

char **cArray = "abc";
char *c = "def";
cArray += &c;
printf("%s", cArray);

Okay, lets take this apart line by line. First you declare a char ** called cArray and initialize it to "abc". Well, your variable cArray is a pointer to a pointer. The value "abc" I believe is a const char*. Therefore, you probably don't want to assign a pointer to a character as a pointer to a pointer. The consequence being, the value "abc\0" will be interpreted as another memory address. This, obviously, will not point to anything useful and accessing this memory would result in a seg fault.

Next, you initialize c to be a cstring containing "def".

Finally, you increment the address pointed to by cArray by whatever address "def" happens to be located at. So now, cArray is no longer even pointing to "abc" at all (nevermind interpreting it incorrectly).

Then we try to print some block of memory pointed to by cArray that is in no way even remotely close to any of the bits our program wants to be touching.

All of this said, say I had two cstrings and I wanted to get a third such that it is the first appended to the second:

char* a = "abc";
char* b = "def";
size_t sizeA = strlen(a);
size_t sizeB = strlen(b);
size_t size = sizeof(char) * (sizeA + sizeB + 1); //Size of our new memory block large enough to contain both a and b. 
                                                  //Leave additional space for null terminator
char* c = malloc(size);                           //Actually allocate new memory
memcpy(c, a, sizeA);                              //Copy a into the first half of c
memcpy(c + sizeA, b, sizeB);                      //Copy b into the second half
c[sizeA + sizeB] = '\0';                          //Assign null terminator to last character
printf("%s", c);
free(c);                                          //Never forget
Mr. Nex
  • 233
  • 1
  • 12
  • Thank you for your help. So this isnt possible, Ive seen similar things on internet but with int, void update(char **ccc, char *c){//append c to ccc} – Greg Apr 09 '16 at 21:36
  • Happy to be of assistance. I don't quite understand what you are looking for with the code snippet though. – Mr. Nex Apr 09 '16 at 21:40
  • A good examplefor this would be. char ** is PATH and char* is a DIR, im trying to add DIR to the end of PATH – Greg Apr 09 '16 at 21:44
  • 1
    If `PATH` has an empty space, say nothing in index 6, you could simply use `PATH[6] = DIR`. However, `PATH` is NOT a string that has `DIR` appended to the end. It is a pointer to (probably an array of- but not necessary) pointers to characters (probably sets of characters contiguous in memory). I suggest looking up images / videos of what pointers are. A visual representation would help clear up confusion I'm sure. If `PATH` does not have any empty space, and you do not want to remove any existing information to make room, you need to allocate more memory with enough room to accomodate it. – Mr. Nex Apr 09 '16 at 21:48