3d array .......

This is a discussion on 3d array ....... within the C Programming forums, part of the General Programming Boards category; Code: *array[0][0]='ass'; this line gives me error , overflow in implicit constant conversion. does that mean this line is wrong? ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    51

    3d array .......

    Code:
    *array[0][0]='ass';
    this line gives me error , overflow in implicit constant conversion.


    does that mean this line is wrong? or my malloc'ing is wrong?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    How did you declare the array, and what are you actually trying to do?

    Also 'ass' is a integer value (that is perhaps 0x617373, but that depends on the implementation of the compiler). It is very rarely a useful way to store characters.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    thnkx for quick response


    Code:
        char ***array = (char ***)malloc(26 * sizeof(char **));  // mallocing one dimension for each alphabetical letter.
            for(i = 0; i <26; i++) {
                array[i] = (char **)malloc(wrdcnt * sizeof(char *)); // mallocing how many words will be contained in each letter dimension
                for(j=0; j<wrdcnt; j++)
                    array[i][j] = (char *)malloc(35 * sizeof(int)); //mallocing the max size of each word, 3 letters max/per word
        }

    I trying to make a 3d char array where it holds words alphabetically.
    Last edited by ktran03; 02-01-2009 at 05:44 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So to move strings around you need strcpy, not =.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Ok, and you are then trying to set one element to an integer value of 'ass', right? And you are suprised that the compiler says "implicit conversion from integer to pointer"?

    If you mean to assign a string, then you should do one of the following (or something similar):
    1. Use strcpy() to place it in the allocated 80-character string.
    2. Not allocate the third level, and assign the string constants directly (not that then you should have const char ***array).
    3. Not allocate the third level until you know what string you are storing, then allocate strlen(str)+1 bytes and use strcpy() to copy the content of the original string into the array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by tabstop View Post
    So to move strings around you need strcpy, not =.
    I have a little algorithm to strcpy, but it doesn't work.

    i removed it, then just putting sentences in the 3d array to see if it's malloc'd correctly.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    See, the error in your original thing was that *array[0][0] is a character variable, and 'ass' is most assuredly not a character. You need to strcpy (into array[i][j], not *array[i][j], 'cause as mentioned *array[i][j] is a character, not a pointer to a bunch of characters).

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by tabstop View Post
    See, the error in your original thing was that *array[0][0] is a character variable, and 'ass' is most assuredly not a character. You need to strcpy (into array[i][j], not *array[i][j], 'cause as mentioned *array[i][j] is a character, not a pointer to a bunch of characters).
    oh crap,

    what am i thinking.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    just tried

    array[0][0] = 'ass';

    I'm getting assignment makes pointer from integer without a cast.

    what does that mean?

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You do realize that 'ass' is illegal, right? If you wanted a string, you would use "ass".

    In this case, you must not assign anything to array[0][0], as you will immediately leak your malloc'ed memory (it's gone, and you can't get it back). If you want to move strings around, you must use strcpy.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    51
    Quote Originally Posted by tabstop View Post
    You do realize that 'ass' is illegal, right? If you wanted a string, you would use "ass".

    In this case, you must not assign anything to array[0][0], as you will immediately leak your malloc'ed memory (it's gone, and you can't get it back). If you want to move strings around, you must use strcpy.
    stupid things I always overlook. dam quotations.


    k, gonna go reimplement the strcpy algorithm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D Array of Bool
    By cybernike in forum C++ Programming
    Replies: 37
    Last Post: 06-28-2007, 12:17 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  3. 3D array
    By GSLR in forum C Programming
    Replies: 2
    Last Post: 05-12-2002, 05:00 PM
  4. 3D Array
    By Alextrons in forum Windows Programming
    Replies: 4
    Last Post: 01-11-2002, 01:39 AM
  5. Dynamic 3D array allocation
    By Vulcan in forum C++ Programming
    Replies: 4
    Last Post: 11-21-2001, 02:51 AM

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