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

This is a discussion on How come this works perfectly? Replacing a 2d character array with 1d array. within the C Programming forums, part of the General Programming Boards category; Hello, I have a test coming up soon I was wondering how come this works. For example, if you needed ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    244

    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. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    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. #4
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Also,
    Can someone help me understand how this works....

    -55 mod 7= 1

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Quote Originally Posted by matthayzon89 View Post
    [...]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.

    Quote Originally Posted by matthayzon89 View Post
    [...]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?
    Last edited by Xupicor; 09-23-2010 at 12:15 PM.

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    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. #8
    a guy with long hair Xupicor's Avatar
    Join Date
    Sep 2010
    Location
    Poland
    Posts
    103
    Quote Originally Posted by matthayzon89 View Post
    [...]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. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    Quote Originally Posted by Salem View Post

    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.
    Last edited by matthayzon89; 09-23-2010 at 01:25 PM.

  12. #12
    Registered User
    Join Date
    Feb 2010
    Posts
    244
    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. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,602
    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #15
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    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.
    Last edited by Salem; 09-23-2010 at 02:20 PM. Reason: Losing the will to give a damn about this thread
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get a 2d character array from a file?
    By DaNxTh3xMaNx in forum C Programming
    Replies: 12
    Last Post: 09-18-2010, 08:18 PM
  2. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03:50 PM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Replies: 6
    Last Post: 10-21-2003, 09:57 PM
  5. Character Array - almost? works
    By voltson4 in forum C Programming
    Replies: 3
    Last Post: 03-04-2003, 05:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21