0

What is the difference between

char (CharBuff[50])[10];

and

char CharBuff[10][50];

My requirement is to have 10 character buffers, each of length 50 max (including null terminating character)

And, I would like to access the buffers as CharBuff[0], CharBuff[1] and so on.

4
  • And yes, use vector and string. Commented Jul 7, 2014 at 10:42
  • 2
    @P0W No, they aren't the same! See here. Commented Jul 7, 2014 at 11:02
  • 1
    @Debasish Jana Please don't tag a question as both C and C++ since there're numerous subtle differences between them in the C subset and huge differences otherwise (both syntactically and idiomatically), so be specific. Commented Jul 7, 2014 at 11:07
  • @legends2k Sorry my bad, I didn't notice 50 and 10 odering Commented Jul 7, 2014 at 11:25

7 Answers 7

2
char (CharBuff[50])[10];

declares CharBuff as a 50-element array of 10-element arrays of char. The parentheses are superfluous in this case.

char CharBuff[10][50];

declares CharBuff as a 10-element array of 50-element arrays of char.

Given that you want 10 strings of up to 50 characters, you would use the second form; the type of each CharBuff[i] will be "50-element array of char".

If you really wanted to create a separate type definition for a 50-element array of char, you could do something like

typedef char Str[50];  
...
Str CharBuff[10];

Now CharBuff is a 10-element array of Str, which is a 50-element array of char.

Normally, I would not create a separate typedef like this unless I wanted to make Str opaque; that is, I don't want to expose the details of its implementation to whomever's using it. In addition to the typedef, I'd also supply an API for allocating, assigning, copying, formatting, and displaying objects of type Str.

Put another way, if the person using the Str type has to be aware that it's a 50-element array of char in order to use it properly, then it's better to just make them use a 50-element array of char.

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

Comments

1

The big problem that i see here is confusing syntax. When you use parenthesis they already have known roles in c and c++ syntax such as type conversions, pointers to functions(which looks a bit like what you wrote). What you are using them for add's a new meaning which makes code more obfuscated and ignores the fact that c and c++ have a great and intuitive way, for anybody that used matrices at least once, to express 2d arrays. So, for a clean and not confusing syntax use the latter version:

char CharBuff[10][50];

1 Comment

got it, for unambiguous-ness, can I declare a typedef for character buffer of 50 characteers, call it as CHAR50, and use CHAR50 CharBuf[10];
1

The other answers here which say they're the same are wrong! Both are NOT the same arrays and their sizes are very different. Here's a snippet to show that:

char i[50][10];
std::cout << sizeof(i[1]) << '\n';

char (j[10])[50];
std::cout << sizeof(j[1]) << '\n';

10

50

You can see the live example here.

i is a 50-element array with each element being a 10-element array of characters, while j is a 10-element array with each element being a 50-element array of characters. Although the total sizes of both would be the same, the size of an element at each level would be different. If you assume they're the same, it would lead to undefined behaviour

i[25][5]  // OK
j[25][5]  // accessing j beyond index 9 is undefined behaviour!

This shows that the parenthesis have no significance in a non-pointer, non-reference array declaration i.e. char (j[10])[50] is just confusing notation for char j[10][50].

My requirement is to have 10 character buffers, each of length 50 max

Then you should declare your array as char CharBuff[10][50].

Comments

0

Nobody ever uses the former, always use the latter form:

char CharBuff[10][50];

Comments

0

Latter syntax will be proficient and less confusing as per you requirement of 10 rows (char buffers) having length of 50 each.

Comments

0

Go for the last one as it clearly states what you are allocating.
However, since the question is tagged with c++ too, I would advise using std::vector and std::string as it follows:

std::vector<std::string> CharBuff(10, std::string(50, '\0'));

4 Comments

Messing around with the null terminator and std::string isn't a good idea in my experience. std::vector<std::vector<char>> CharBuff(10, std::vector<char>(50)); is better.
@NeilKirk Unfortunately, you must provide the character to be filled in. Although it can be useful sometimes, in this case it might not. Anyway you can simply use resize inside a range-for.
If you create std::string(50, '\0'), what whould std::string::size return?
50. std::string does not work with '\0' as C-style "strings" do.
-1

You are looking for 10 arrays of 50 chars each.

To declare a single buffer of 50 bytes, you would use

char CharBuff[50];

But you want to have 10 buffers, so just tack that on to before[50], e.g.:

char CharBuff[10][50];

Now CharBuff[0] will address the first fifty-byte buffer, CharBuff[1] will get the second, and so on.

1 Comment

CharBuff[0] addresses the first ten byte array. Just do a sizeof(CharBuff[0]) and you'll see...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.