# Thread: How come this works perfectly? Replacing a 2d character array with 1d array.

1. ## How come this works perfectly? Replacing a 2d character array with 1d array.

Hello,
I have a test coming up soon I was wondering how come this works.

For example, if you needed to read in a list of names from an input file and store them into an array, its normal for one to think to use a 2d array and just address the rows of the array to access each name.

A programmer that I know showed me that this can be done with a 1d array, how is this possible?

in the declaration he declares the 1d array as follows:

char* names[5];

(or something similar)

This does not work without the pointer after char.

and he can access an entire string in 1 position in the character array which makes no sense to me.

2. Well it doesn't, unless you follow up with something like

names[0] = malloc(100);

Then you can write up to 99 chars into the memory that names[0] points to.

If you do that for all the pointers, you've effectively created
char names[5][100];

But if you DON'T do these things, then your "friend" is playing with fire, and it's only a matter of time before it all goes so horribly wrong.

3. he did not use malloc....

It worked! I saw it! Even though I explained why it should not work and it shouldnt be done, and the best way to going about it is a 2d array... he showed me that it compiled and he was able to ACCESS an entire string from calling 1 position in a 1d array...

I was wondering how is this even possible, even if it obviously not good programming...

4. Also,
Can someone help me understand how this works....

-55 mod 7= 1

5. > It worked! I saw it!
Yes, so have I, many times.
It doesn't make you a good programmer though, just a lucky one.

> I was wondering how is this even possible
Nothing more than pure dumb luck.

Hey, it's your exam - put what you like and see what happens.

6. Originally Posted by matthayzon89
[...]its normal for one to think to use a 2d array [...]
A programmer that I know showed me that this can be done with a 1d array [...]
Code:
`char* names[5];`
Well, what you see there is 5 element array of pointers to char, which effectively can make 2d char array, but you have to allocate memory for every char* element, or you won't be using it properly. (Also, you can allocate different amounts of memory for each element - see argv?)

It's not that different from char** names or char names[][] if you think about it (yes, I'm simplifying a bit).

If your friend is not allocating memory properly, I can't see how the program would even work consistently, without a segfault from time to time (or all the time). So he did, but you didn't see where and how, or he didn't and had a dumb luck, like Salem said.

Originally Posted by matthayzon89
[...]and he can access an entire string in 1 position in the character array which makes no sense to me.
So you see, you're mistaken, its not that he can access entire string in 1 position of the char[] - because it's not char[], it's char* [] - not a char array, but array of pointers to char - you could say, array of c-strings. Now does it make sense?

7. No need to get moody, I was asking out of curiosity, to increase my comprehension of how c works. You explanation which is "Nothing more than pure dumb luck" translates to "I dont know why it works"..... Once again, I am not planning on using it, I was just curious about it.

Also, does anyone know how this works?
Determine φ(77) *Euler Totient function for 77+.

I am trying to figure it out....

I know that starting point is 77= 11 * 7 (the highest integers that can be multiplied to get 77)...
then I dont know what to do.

Our professor is terrible at going over concepts. Then we get a practice exams that do not reflect his notes and lectures. Gr.

8. Originally Posted by matthayzon89
[...]You explanation which is "Nothing more than pure dumb luck" translates to "I dont know why it works".....
I know it wasn't pointed against me, but let me answer.
I'm sure Salem has far more knowledge on C and internals than I do, but I think I have some idea why that can sometimes work.
You see, when you create new array of pointers to char, you get the memory for that array, that memory has some "random" values that were there before, unless you initialize the array to some values.
Now, these values are interpreted as pointers to char, and they coincidently may point to "valid" places in memory. If you write to that pointed memory, you can end up crashing your program, but you may also get lucky, and not crash it. The point is, even if it works at times, you have absolutely no guarantee that it will work the next time you run your application. It could even break your application silently, overwriting other variables' memory, while you think everything is in perfect order.
All kinds of stuff could happen.

Hopefully someone will correct me if I'm wrong.

9. Who cares "why" it works when it's wrong?

Yes, if you posted the exact code, I could probably give you a plausible (and contrived) answer as to why it works IN THAT SPECIFIC CASE.

But it isn't any knowledge that will do you any good in future. You couldn't take it and apply it to all your programs and have success every time.

I'm only interested in showing you how to do it right, not explain myriads of exceptional "well it works for me bub" programs which are plainly wrong to all but the author.

If you don't want to learn, just listen to your "friend" and stop posting.

10. Originally Posted by matthayzon89
A programmer that I know showed me that this can be done with a 1d array, how is this possible?
That said, this really can be done with a 1D array of char: you simply manually break up the one long array into parts.

11. Originally Posted by Salem

If you don't want to learn, just listen to your "friend" and stop posting.
If you read my older post on this thread you would see that I am asking because I DO want to learn. I know my friend is wrong, like i said earlier in my other posts and I am not planning on using this. I was simply curious to WHY it works.

Why do you even bother replying ......?

..... What does "it works because of 'dumb luck' " teach me about why c programming compiles the code with no errors? NOTHING.

If your 'teaching methods' involves being a jerk towards me, when I ask a completely reasonable question, than I have no interest in learning anything from you.

12. LaserLight thanks for your help.
So, once it breaks it up into parts, how is it successful in accessing a whole string via 1 array slot?

13. Originally Posted by matthayzon89
So, once it breaks it up into parts, how is it successful in accessing a whole string via 1 array slot?
Perhaps an example will illustrate the idea:
Code:
```#include <string.h>
#include <stdio.h>

int main(void)
{
char strings[12];
strcpy(&strings[0], "hello");
strcpy(&strings[6], "world");
printf("%s\n%s\n", &strings[0], &strings[6]);
return 0;
}```
Of course, this is quite silly, but it does have a sensible practical application: maybe you want a dynamic 2D array. You could have a dynamic array of pointers, and then allocate a dynamic array for each pointer. Or, you could allocate one big dynamic array, and then allocate another dynamic array of pointers, and then get those pointers to point to the parts of this one big dynamic array, and thus be able to access it as if it were a "normal" 2D array. Ultimately, a "normal" fixed size 2D array is also one fixed size array that the compiler has divided for you.

14. What do you mean by "one array slot"?

char *words[5];

Would create an array of 5 pointers to char - in other words, a type of 2D char array. It's nothing new. It's mentioned in the bible of C "The C Programming Language" by Kernighan and Ritchie.

Although this may work on some compilers and OSs, it's irrelevant. When you go through a lot of work to make a good program, you gain the perspective that you want to treat this program as well as you can. For a "10 minutes to code up" utility program that you'll only run once, you might use something like this - but for something you'll really be proud of, and want to run more often, and possibly build onto later - no way!

Definitely NOT for something for a customer, or production in-house! I believe that's where Salem (and lots of others) are coming from on this. It's fine for Tic-Tac-Toe or "Fun with Palindromes" or something like that, but from a professional viewpoint you'd be embarrassed to have that kind of code in your program.

And if you are seeing it from that perspective, then look a tad further and ask yourself "why would a good programmer ever get into the habit of using code like that?". Are you kidding me? They wouldn't touch it with a clean stick, on the driest day of the year!

Whether it works for him or you, or me, is irrelevant. There's a big human factor here.

Good programmers want to see their work as the product of someone "standing on the shoulders of giants". Not the product of someone "mashing down on the toes of midgets".

There are LOTS of quirky stuff like this that you'll run into with C, btw. Some of it is really ???, other parts are just ROFL.

15. Until you post some actual code from your "friend" which apparently works for them, then there ain't much anyone here can do to help you figure it out.

And I mean an actual "working" program, not a 1-line declaration and a random bunch of words sounding like they were plucked straight from the ass of some politician.