Multidimensional arrays returning.

This is a discussion on Multidimensional arrays returning. within the C Programming forums, part of the General Programming Boards category; People, don't cast the return of malloc. If you assign a valid pointer from malloc to your pointer, you will ...

  1. #16
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    People, don't cast the return of malloc.
    If you assign a valid pointer from malloc to your pointer, you will not get a warning. If you do, however, you WILL get a warning, that tells you that you did wrong.
    If you cast the return, you will NOT get a warning that says you did something wrong, so do not cast malloc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by vlrk View Post
    The Output for your program is

    Code:
    smart,hard
    pass,fail
    
    You are smart
    
    Output:You
    Output:are
    Output:smart
    MATCH! smart = smart -> hard
    Is this is the same output which is as per design.

    I thought you must get like

    If it is

    Code:
    You are smart

    Code:
    You are stupid
    where database.txt is

    Code:
    smart stupid
    and hi.txt is

    Code:
    You are smart

    It wouldn't be hard enough to write to the file.

    > anon, I've got 2 questions:
    Say that I have a string:
    char *str = "Hello world";
    Am I supposed to null-terminate this string? you haven't done it in your code..

    Could you answer my former questions?
    I should allocate memory to each pointers I use (in case he is not pointing to any variable)? I'm assuming that yes since pointers that are not initialized point to a random memory address.
    And what about arrays...should I allocate memory for them or they are automatically allocated in the stack by the compiler?
    > Elysia, what's your point?
    Last edited by eXeCuTeR; 07-01-2008 at 06:02 AM.

  3. #18
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by eXeCuTeR View Post
    Say that I have a string:
    char *str = "Hello world";
    Am I supposed to null-terminate this string? you haven't done it in your code..
    It's automatically null terminated, but it should be const char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Elysia View Post
    It's automatically null terminated, but it should be const char*.
    What about char * and why specifically const char *?
    In case of this very simple program:
    Code:
    char *name = (char *)malloc(20);
    *name = "Hello"; // is this even legal?
    *(name + 6) = "\0"; // should I do this?
    Please answer my previous questions too, thanks.

  5. #20
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Um, no that's not legal. Use strcpy().

  6. #21
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Your code is illegal. Again, do not cast the return of malloc.
    Better code would be:
    Code:
    char *name = malloc(20);
    strcpy(name, "Hello");
    Last edited by Elysia; 07-01-2008 at 06:37 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Elysia View Post
    Your code is illegal. Again, do not cast the return of malloc.
    Better code would be:
    Code:
    char *name = (char *)malloc(20);
    strcpy(name, "Hello");
    Why is my code illegal, what's wrong with it?
    About the return-casting...you are telling me not to cast the return value of malloc but you are casting it yourself...am I missing something?

  8. #23
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by eXeCuTeR View Post
    Why is my code illegal, what's wrong with it?
    Because a string literal is const char*. You are trying to assign a pointer in your buffer, which is not the same as copying the data.

    About the return-casting...you are telling me not to cast the return value of malloc but you are casting it yourself...am I missing something?
    Sorry, I just copied your code and forgot to remove it. I fixed it.
    (Note that if you are using C++ to compile the code, you need the cast, but if you are compiling it as C, no cast is necessary.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #24
    The larch
    Join Date
    May 2006
    Posts
    3,573
    About null-terminating:
    most if not all standard library string functions null-terminate the string (and expect a null-terminated string).
    Literal strings are null terminated by the compiler:
    Code:
    const char* str1 = "Hello";
    //str1 points to a non-modifiable null-terminated string
    char str2[] = "world";
    //str2 points to a modifiable null-terminated string
    But if you manipulate the individual characters yourself, naturally you must null-terminate the string. E.g
    Code:
    char str[10];
    str[0] = 'a';
    str[1] = 'b';
    str[2] = 'c';
    
    /*str[3] does not necessarily contain '\0' here, since str initially contains uninitialized garbage*/
    /*hence:*/ 
    str[3] = '\0';
    Or you can initialize the buffer to all '\0'-s to begin with, in which case str[3] would also contain '\0':
    Code:
    char str[10] = {};
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  10. #25
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Code:
    char str[10] = {};
    Would initialize all of the cells in str to \0?
    ---
    Why does this code work?
    Code:
    char *str = malloc(20);
    str = "Hello world"; // how come this works?
    printf("%s", str);
    How come the 2nd line works? Shouldn't I add '*' before str to access its value and not its address?

    One more thing..
    Assuming I have a pointer that points to the stack: what does &p contain? I mean, p holds the address, and *p holds the value, what else could &p hold? (it's a pointer to a pointer, right?)
    Code:
    int hi = 5;
    int *p = &hi;
    BTW, what's %i?
    Last edited by eXeCuTeR; 07-01-2008 at 07:17 AM.

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by eXeCuTeR View Post
    Code:
    char str[10] = {};
    Would initialize all of the cells in str to \0?
    Yes, all elements are initialized to 0.

    Why does this code work?
    Code:
    char *str = malloc(20);
    str = "Hello world"; // how come this works?
    printf("%s", str);
    How come the 2nd line works? Shouldn't I add '*' before str to access its value and not its address?
    This code is flawed and bad.

    A string literal, such as "Hello world" is a pointer to const char (const char*).
    Typically, the compiler will store that string somewhere inside the EXE and when you try to assign it somewhere, it fetches that address and stuffs it into a pointer.
    So what happens in your example above?
    Also remember that dereferencing a pointer gives you the value. However, an array is values (many, not single). Therefore, you will get only ONE value, and not the entire.
    In other words, dereferencing char*, will give you char, not a string. What happens then?
    sizeof(char*) is probably 4 and sizeof(char) is 1, so in that sense, even there you are doing something fatefully wrong.

    One more thing..
    Assuming I have a pointer that points to the stack: what does &p contain? I mean, p holds the address, and *p holds the value, what else could &p hold?
    Code:
    int hi = 5;
    int *p = &hi;
    &p is the address of the pointer, simply put. A pointer is still a variable and thus has an address. The resulting type would be int** (a pointer to int*).
    Last edited by Elysia; 07-01-2008 at 07:21 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #27
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by eXeCuTeR View Post
    Why does this code work?
    Code:
    char *str = malloc(20);
    str = "Hello world"; // how come this works?
    printf("%s", str);
    How come the 2nd line works? Shouldn't I add '*' before str to access its value and not its address?
    What happens in this code:
    1) 20 char's are allocated and str is a pointer to this memory
    2) Second line makes str point to a unmodifiable literal string instead. Previously allocated memory is leaked: you don't have a pointer to it anymore, meaning your program cannot use this memory and cannot deallocate it either.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  13. #28
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Strings and literal characters (static strings etc') are located in a "special" place and don't have an address, this is the point, right?

    If so..
    Code:
    char *name = malloc(20);
    fgets(name, 20, stdin);
    This is not considered literal and should be constant?

  14. #29
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by eXeCuTeR View Post
    Strings and literal characters (static strings etc') are located in a "special" place and don't have an address, this is the point, right?
    String literals only, but they DO have an address.
    Without it, it would be unusable.
    The point is that the compiler can store the literals there and point to them instead of creating code to create these strings everytime you use them.
    That is the point.

    If so..
    Code:
    char *name = malloc(20);
    fgets(name, 20, stdin);
    String literals are non-modifiable and should therefore be const to make sure you don't modify them.
    But if you apply const to name here, what would happen? You can't modify the data you allocated, thus defying the point of doing it in the first place.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #30
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Elysia View Post
    String literals only, but they DO have an address.
    Without it, it would be unusable.
    The point is that the compiler can store the literals there and point to them instead of creating code to create these strings everytime you use them.
    That is the point.


    String literals are non-modifiable and should therefore be const to make sure you don't modify them.
    But if you apply const to name here, what would happen? You can't modify the data you allocated, thus defying the point of doing it in the first place.
    How do I check theirs addresses?

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 05-29-2009, 05:48 AM
  2. Multidimensional Arrays
    By jordanguyoflove in forum C Programming
    Replies: 4
    Last Post: 10-16-2008, 06:16 PM
  3. Multidimensional Arrays?
    By CreatedByShadow in forum C++ Programming
    Replies: 7
    Last Post: 01-13-2006, 09:35 PM
  4. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  5. Multidimensional arrays in Korn shell
    By Yasir_Malik in forum Tech Board
    Replies: 3
    Last Post: 04-11-2004, 02:16 PM

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