some help needed->size of 2D array

This is a discussion on some help needed->size of 2D array within the C Programming forums, part of the General Programming Boards category; Hi, I would definitely use some help on this: I've got a list of words in a .txt file. 1 ...

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    13

    some help needed->size of 2D array

    Hi, I would definitely use some help on this:
    I've got a list of words in a .txt file. 1 word for 1 line. First I count the \n's to get the number of words and then I put the words to the array I defined by doing(size is number of words):
    Code:
    char chDizi[size][22];
    do{
            fscanf(ptDosya01,"%s",&chDizi[j]);
            j++;
            }while(j<size);
    now as you see, the size of the second dimension of the array is 22 which is a limit for me and that is my problem. I have words longer than 22 char.'s, so I need to define the size as 30 or so. And if I put a number bigger then 22(23 for example) , I get an error message from the system (not the compiler) saying my program needs to be shut, when I try to run it.

    So, I'll be very thankful if you help me find why this happens and what should i do. Thanks very much.
    Last edited by creon; 09-12-2006 at 12:25 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,855
    > char chDizi[size][22];
    Well this isn't legal C either - you're relying on compiler extensions.

    The first step would be
    Code:
    char **lines = malloc ( size * sizeof *lines );
    Then your loop would be
    Code:
    do{
        char buff[BUFSIZ];
        fscanf(ptDosya01,"%s",buff);
        lines[j] = malloc( strlen(buff) + 1 );
        strcpy( lines[j], buff );
        j++;
    }while(j<size);
    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
    Oct 2001
    Posts
    2,934
    >I get an error message from the system (not the compiler) saying my program needs to be shut
    Either:
    (1) You're using an old compiler (like Turbo C), or
    (2) you've got a bug in your program.
    If it's (1), you'll either have to declare the array global, or using dynamic memory allocation. Something similar to this:
    Code:
    char *chDizi[size];
    int j = 0;
    do {
       fscanf(ptDosya01, "%s", word);
       chDizi[j] = malloc(length_of_word + 1);
       strcpy(chDizi[j], word);
       j++;
    } while(j<size);
    If it's (2), fix the bug.
    Last edited by swoopy; 09-12-2006 at 12:45 PM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    Thnx for your answers. When I use either of the ways, I get the same error message " invalid conversion from `void*' to `char*' " for the line with the command malloc. Do you know why thi is?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    well, i guess i've solved my problem. i needed to add (char*) just before malloc. Thank you very much for helping me out.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by creon
    Thnx for your answers. When I use either of the ways, I get the same error message " invalid conversion from `void*' to `char*' " for the line with the command malloc. Do you know why thi is?
    Because you're compiling your C program as C++.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by creon
    Hi, I would definitely use some help on this:
    I've got a list of words in a .txt file. 1 word for 1 line. First I count the \n's to get the number of words and then I put the words to the array I defined by doing(size is number of words):
    Code:
    char chDizi[size][22];
    do{
            fscanf(ptDosya01,"%s",&chDizi[j]);
            j++;
            }while(j<size);
    now as you see, the size of the second dimension of the array is 22 which is a limit for me and that is my problem. I have words longer than 22 char.'s, so I need to define the size as 30 or so. And if I put a number bigger then 22(23 for example) , I get an error message from the system (not the compiler) saying my program needs to be shut, when I try to run it.

    So, I'll be very thankful if you help me find why this happens and what should i do. Thanks very much.
    Have you tried?

    [code]
    char chDizi[size][40];
    do{
    fscanf(ptDosya01,"%s",&chDizi[j]);
    j++;
    }while(j<size);
    [/code

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by creon
    well, i guess i've solved my problem. i needed to add (char*) just before malloc. Thank you very much for helping me out.
    That's not the best solution. You're still compiling your C program as C++. You'll likely end up running into other unexpected problems if you continue down this path.

    The best fix would be to make sure you're actually compiling your program as a C program. Different IDEs handle this differently. Maybe it's in project options, maybe you just need to make sure your source code filename ends in .c instead of .cpp.
    If you understand what you're doing, you're not learning anything.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Quote Originally Posted by esbo
    Have you tried?

    [code]
    char chDizi[size][40];
    do{
    fscanf(ptDosya01,"%s",&chDizi[j]);
    j++;
    }while(j<size);
    [/code
    It's obvious he's just running out of stack space. What exactly is your solution supposed to solve? Dynamic memory allocation, as Salem already pointed out, is the correct solution.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by itsme86
    It's obvious he's just running out of stack space. What exactly is your solution supposed to solve? Dynamic memory allocation, as Salem already pointed out, is the correct solution.
    It might be 'obvious' to you but it's not to me. Could you explain why it is 'obvious'?

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Experience. Go read up on how memory is laid out and how the stack works. He could definitely run out of stack space very quickly if, like swoopy mentioned, he's using an older 16-bit compiler.

    <sarcasm>From the way you don't take advice from anyone, I assumed you had a higher level of knowledge and experience.</sarcasm>
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    13
    yeah, i saved it as .c and deleted the (char*) part and it works corectly. thanks. But can you explain what "my way" does? Since I've copied it from an other source code, I don't know what it does(the (char*) thing..). Thanks.

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    It's called typecasting. Basically you're telling the compiler to treat whatever it is you're typecasting as the type you're casting it as. In your case, you were telling the compiler that the return type of malloc() was char * which is not necessary in C.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Connection between 2D array and list vector?
    By glo in forum C++ Programming
    Replies: 5
    Last Post: 08-31-2008, 02:53 PM
  2. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  3. Allocating a 2D Array Differently
    By pianorain in forum C++ Programming
    Replies: 13
    Last Post: 12-15-2005, 02:01 AM
  4. 2D array project help
    By Cnewbie in forum C Programming
    Replies: 2
    Last Post: 12-10-2001, 09:20 PM
  5. how to pass 2D array into function..?
    By IngramGc in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2001, 09:41 AM

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