-2

Why wouldn't my character array store the Strings? If I declare an character array with a specific size and then store a string inside that array, wouldn't I be able to create a 1-D array with a specific length to store multiple Strings? Since a string is considered a character array and Strings literals are null terminated I would need to account for all the chars + 1, if I write

char a[3] = "Hi"; char b[4] = "Bye";

Then char arrays are created with elements

a[0] = 'H', a[1] = 'i', a[2] = '\0' and b[0] = 'B', b[1] = 'y', b[2] = 'e', b[3] = '\0'

These are still a 1 Dimensional Arrays.

If I create an array char array[2] = {a[3], b[4]} isn't this still a 1 Dimensional Array? So the Contents of array[2] = {"Hi", "Bye"}.

Technically array[2] would be

array[0]= a[0] = 'H', a[1] = 'i', a[2] = '\0' and array[1] = b[0] = 'B', b[1] = 'y', b[2] = 'e', b[3] = '\0'

This technically isn't array arithmetic nor multidimensional array construction right? The character arrays aren't being combined, nor forming a second dimension??

EDIT 1: I know it can be done in multidimensional array, but I was wondering was would it be possible in 1? If I initialize an array with 2 elements, hence array[2], For the elements inside I initialize another character array with 3 and 4 elements.

So technically it's a 1x7(composed by a 1x3 and 1x4) matrix compared to 2x41

Edit 2: Why I think I would want a 1 dimensional matrix is since it's 1 dimensional, and I know the range of which elements correspond to a definition, I could just look at specific elements within the sequence instead of doing multiple dimensions

Edit 3: When I try and use the dereference operator it says runtime error but it points to the whole array and not element by element? Is my array with the dereference operator still not a 1x7 array but just a 2 element array in which the element then points to the char array with actual values inside?

    #include <iostream>
using namespace std;

int main() {
    char array1[3] = "Hi";
    char array2[4] = "Bye";
    char* array[2]= {array1, array2};
    for(int i = 0; i <6 ; ++i) {
        cout << "array[" << i << "]= " << array[i]; 
            cout << endl;
    }
return 0;
}

Demo

8
  • char array[2] = {a[3], b[4]} is wrong and causes undefined behavior and all you write about array is wrong. To save two text strings you an array of arry (e.g. array[2][4]) Commented Oct 4, 2018 at 19:04
  • 1
    char array[2] = {a[3], b[4]} is out of bounds access for a and b. For examples sake lets use valid indexes 1 and 2: char array[2] = { a[1], b[2] }; --> char array[2] = { 'i', 'e' }; Commented Oct 4, 2018 at 19:05
  • Please look here: stackoverflow.com/questions/6599707/… , but surely there are better dupes somewhere... Commented Oct 4, 2018 at 19:05
  • 1
    An array that *contains* both strings: char array[2][4] = { { a[0], a[1], a[2], 0 }, { b[0], b[1], b[2], b[3] } }; Commented Oct 4, 2018 at 19:08
  • 1
    Of course you can write the elements of a and b to an array of sizeof(a) + sizeof(b) bytes: char array[] = { a[0], a[1], a[2], b[0], b[1], b[2], b[3] };. If thats what you want depends on what you want to do with it. Commented Oct 4, 2018 at 20:13

1 Answer 1

3

If I create an array char array[2] = {a[3], b[4]} isn't this still a 1 Dimensional Array?

Yes.

So the Contents of array[2] = {"Hi", "Bye"}.

Technically array[2] would be

array[0]= a[0] = 'H', a[1] = 'i', a[2] = '\0' and array[1] = b[0] = 'B', b[1] = 'y', b[2] = 'e', b[3] = '\0'

No. array can only hold 2 chars. Initializing it with a[3] and b[4] is accessing 2 individual chars from a and b (accessing them using out-of-bounds indexes, mind you! Remember, arrays use 0-based indexing). You cannot store complete strings "Hi" and "Bye" into a 1-dimensional array that can only hold 2 chars.

What you are thinking of can be accomplished using this instead:

char* array[2] = {a, b};

This technically isn't array arithmetic nor multidimensional array construction right? The character arrays aren't being combined, nor forming a second dimension??

Correct. The above is creating one array in memory, whose elements are pointers to other arrays elsewhere in memory.

If you really wanted a true 2-dimensional array, it would have to look more like this instead:

char array[2][4] = {"Hi", "Bye"};

Live demo

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.