Putting a char pointer into a 2d char array

I am trying to put say a list of words into a 2d array. I can get the pointer dereferenced onto the word. However, when I try output the 2d array it gives me a bunch of blank spaces. The code I have is:

1
2
3
4
5
6
7
8
9
10
11
12
  if (names.is_open()) 
{
 
    names >> limit;  /* limit is a char pointer that is defined as = new char [255] or in other words pointing at whatever word comes in the txt file. */
    cout << limit << endl; /*  <-- I do get the correct output when I just bring up limit */
    length = strlen(limit); 
    cout << length << endl; 
    name[counter][length] = *limit; /*  <-- Based upon the output which is usually blank spaces this is where the code is going wrong. Not sure if I should have another nested loop here so length is a limiter or I am using the wrong function. */ 
    for (a = 0; a < lenght; a++) {
    cout << name[i][a] << endl;
    }
}
name[counter][length] = *limit;

I don't know what you're expecting this to do... but it isn't doing it.

This will copy the first character in the 'limit' string. It will not copy the whole string, if that's what you're intending.


Do yourself a favor and use actual strings instead of char arrays and pointers. It's really much, much easier, and a lot safer.

1
2
3
4
5
6
string limit;
names >> limit;

cout << limit.length() << endl;

name[counter] = limit; // assuming 'name' is a 1D array of strings 
i guess that name is a 3D char array, am i right?
Nay it is a 2d char array. However, on that note I realize using strings is safer and easier but, I am limited against creating strings. My intention was too copy the entire string into the 2d array such as one word would fill name [0][length] where length would be total space the word needs. However, would a nested loop work? such as:

1
2
3
4
5
/* Have the counter loop higher up. So I could scan the next word.
 but to focus on  the interior loop, assumeing counter stays at 0 for this loop. */
for (z = 0; z < length; z++) {
      name[counter][z] = limit; 
}

would that copy the entire string into that array? Or is there a better way of doing this?
Last edited on
no, that mustn't even compile, because you're assigning a char* to char, if you have to do it with C strings, i would recommend to use strcpy() :

1
2
3
4
5
for( int i=0 ; i < NUMBER_OF_STRINGS ; i++)
{
    names>>limit;
    std::strcpy(name[i] , limit);
}


NUMBER_OF_STRINGS would be a const int that you declare, and it equals to the number of strings that name can hold.
However, I am trying to keep it only chars not strings. Would say making a 1d char array = to the pointer work. I guessing that might be conversion error? (sorry still working on the pointer logic) or perhaps it would be better to say bah to the pointer and just go like:

1
2
char namearray [255]  // Random max value. 
names >> namearray


and the I guess write a loop that would scan each character and assign them to a particular position in the 2d array like:

1
2
3
4
5
6
7
8
9
10
11
12
char nameslist [num_of_strings][255]; //then later delete excess elements? 
int i;
int length;
length = strlen(namearray);
for (counter = 0; counter < num_of_strings; counter ++) 
      { 
         for(int i=0; letter[i] < length ;i++) 
             {
                 temp[counter][i] = namearray[i];
              }
        }

I think that would work if I place the loops right, but I was kinda trying to find the short handed way of doing so.
@kingkong200
there's something you should know about programming:
almost all problems can be solved in multiple different ways.

please make up your mind, do you want the pointer or not ?

BTW : the above code.
http://www.cplusplus.com/forum/beginner/106049/#msg573556
it may not give you the required result.

it will copy the the same string to every row in temp, i don't think that's the needed result.
Ahh yea I realized the first for loop would need to be higher up just before the scan of the word is done otherwise it would just repeat the word over and over again. To be honest, I would like use pointers as it seems to me at least the most efficient manner in terms of both effort and memory wise as to why I tried to solve it that way, but it doesn't do me much good to use something that apparently I don't really have a fully understanding of yet (even though I thought I did). Therefore, if it is my understanding of pointers is still too flawed then perhaps I need to back of of them and study a bit more and just go with the loop. However, now realizing what I was trying to do you think a pointer would be more efficient?
@kingkong200
do you think a pointer would be more efficient?


this question gets asked alot.
using array subscripting operator is considered a function call in C++.
a function call has some overhead.
using pointer arithmetic to access array elements is more efficient for performance, but if not carefully examined can generate nasty bugs.

the same is when using dynamically allocated classes like ( string, vector...)
the members of these classes are quite complicated, calling them will sure generate overhead, if you look to squeeze performance out of your program, you might consider using raw pointers, and pointer arithmetic.

keep in mind:
about 1->10 000 function calls can be acceptable, and would add only little to running time, using standard container classes is good in this situation and doesn't contain a lot of overhead.
but let's say you need to cycle through 10 000 000 elements and do some processing on them.
if you use a vector, it can be considerable overhead, here you might consider using pointer arithmetic and normal arrays to avoid all this overhead and squeeze every drop of performance out of your program.
using array subscripting operator is considered a function call in C++.
a function call has some overhead.
[snip]
the members of these classes are quite complicated, calling them will sure generate overhead, if you look to squeeze performance out of your program, you might consider using raw pointers, and pointer arithmetic.


There is no overhead if the function is inlined.

When used properly, and in an optimized build, std::string can be just as fast as doing the dynamic allocation manually.
I see awesome thanks!
Topic archived. No new replies allowed.