Thread: Pointers As 2D arrays

  1. #16
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    try changing "bool" to "_Bool" and use -std=c99 with gcc

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    _Bool is non-standard.
    bool exists in C99.
    Otherwise (if you don't use or can't use C99), just make it int or...
    typedef int bool;
    ...and it should work.
    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.

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    _Bool is non-standard.
    bool exists in C99.
    Actually, _Bool is standard with respect to C99. bool is available in C99 via <stdbool.h>, in which case it is a macro that expands to... _Bool.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah. Well then, why bother at all with _Bool?
    Use bool and you automatically have compatibility with C++, as well.
    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.

  5. #20
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    Alright, nice - the method compiles with the addition of the <stdbool.h> library. It is still giving me a warning. I'm not sure why the compiler is telling me I'm trying to redefine memmove(). I am including <string.h>, which is where memcpy and memmove are supposed to be defined.

    Code:
    //arrayOfNames[m] = malloc(sizeof(name) + 1);
    memmove(arrayOfNames[m], name, (sizeof(name) + 1) );
    ...and the compiler says
    Code:
    In function ‘enterName’:
    36: warning: incompatible implicit declaration of built-in function ‘memmove’
    I've tried type casting the arguments to (void *) with no luck, and really if they're void * it should take any type of pointer correct?

    Also, should I place the malloc prior to this back into the code? The one that I commented out because I didn't think I should need it with memmove or memcpy.

  6. #21
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    okay, so even with the warning about memmove it still compiles, but it's giving me output that's truncated from what string it's supposed to store like the following:

    Code:
    CREATED ARRAY AT MEM LOCATION: 0x83ad008
    
    m = 0
    
    Found a free spot to store: Gavin at [0]
    
    Looking through: Gavin
    
    m = 1
    
    Found a free spot to store: Jessica at [1]
    
    Looking through: Gavin
    
    Looking through: Jessi
    
    m = 2
    
    Array is full!  Stop trying to add stuff!  Your Mom won't fit!
    
    removing "Gavin"
    
    removing "Jessi"
    
    DESTROYING ARRAY AT MEM LOCATION: 0x83ad008
    and here's how I'm trying to enter the names into the array -

    Code:
    bool enterName(char* name, int numSlots, char** arrayOfNames) {
      int m;
      for(m = 0; arrayOfNames[m] != NULL && m < numSlots; m++){
        printf("\nLooking through: %s\n", arrayOfNames[m]);
      }
      printf("\nm = %d\n", m);
      if(m >= numSlots) {
        printf("\nArray is full!  Stop trying to add stuff!  %s won't fit!\n", name);
        return false;
      } else {
        printf("\nFound a free spot to store: %s at [%d]\n", name, m);
        memmove(arrayOfNames[m], name, (sizeof(name) + 1) );
        return true;
      }
      return false; // If you made it this far there was probably no name entered.
    }
    
    int main(int argc, char **argv) {
      char **myArr;
      int arraysize = 2;
      myArr = createArray(arraysize);
      enterName("Gavin", arraysize, myArr);
      enterName("Jessica", arraysize, myArr);
      enterName("Your Mom", arraysize, myArr);
      destroyArray(arraysize, myArr);
      exit(0);
    }
    All of the later names entered are cut off to 5 letters. I tried adding "Jessica" first but it still cuts it down to 5 letters. I've tried this with larger arrays, but thought I'd shorten up the output for posting.
    Last edited by TieFighter; 03-17-2010 at 11:08 PM.

  7. #22
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    sizeof(name) is 4 so you are moving 5 chars

    use strcpy for copying strings
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #23
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    That definitely seemed to work for storing my strings in the array. Thank you. Now... I get an error when trying to free a string that is too large for some reason. I'm not sure if there are any other options for freeing memory. Output ends with this:

    Code:
    removing "My cat Fritzs left front paw claw which is really sharp"
    *** glibc detected *** ./arr2: free(): invalid next size (fast): 0x0913a050 ***
    ======= Backtrace: =========
    /lib/tls/i686/cmov/libc.so.6[0xa73ff1]
    /lib/tls/i686/cmov/libc.so.6[0xa756f2]
    /lib/tls/i686/cmov/libc.so.6(cfree+0x6d)[0xa787cd]
    ./arr2[0x804861d]
    ./arr2[0x804870b]
    /lib/tls/i686/cmov/libc.so.6(__libc_start_main+0xe6)[0xa1fb56]
    ./arr2[0x8048421]
    ======= Memory map: ========
    0014d000-0014e000 r-xp 00000000 00:00 0          [vdso]
    00608000-00624000 r-xp 00000000 08:01 8814598    /lib/libgcc_s.so.1
    00624000-00625000 r--p 0001b000 08:01 8814598    /lib/libgcc_s.so.1
    00625000-00626000 rw-p 0001c000 08:01 8814598    /lib/libgcc_s.so.1
    00a09000-00b47000 r-xp 00000000 08:01 8832339    /lib/tls/i686/cmov/libc-2.10.1.so
    00b47000-00b48000 ---p 0013e000 08:01 8832339    /lib/tls/i686/cmov/libc-2.10.1.so
    00b48000-00b4a000 r--p 0013e000 08:01 8832339    /lib/tls/i686/cmov/libc-2.10.1.so
    00b4a000-00b4b000 rw-p 00140000 08:01 8832339    /lib/tls/i686/cmov/libc-2.10.1.so
    00b4b000-00b4e000 rw-p 00000000 00:00 0 
    00ecf000-00eea000 r-xp 00000000 08:01 8814615    /lib/ld-2.10.1.so
    00eea000-00eeb000 r--p 0001a000 08:01 8814615    /lib/ld-2.10.1.so
    00eeb000-00eec000 rw-p 0001b000 08:01 8814615    /lib/ld-2.10.1.so
    08048000-08049000 r-xp 00000000 08:01 7888989    /home/penguin/Desktop/cs442/Symtab Assignment/arr2
    08049000-0804a000 r--p 00000000 08:01 7888989    /home/penguin/Desktop/cs442/Symtab Assignment/arr2
    0804a000-0804b000 rw-p 00001000 08:01 7888989    /home/penguin/Desktop/cs442/Symtab Assignment/arr2
    0913a000-0915b000 rw-p 00000000 00:00 0          [heap]
    b7700000-b7721000 rw-p 00000000 00:00 0 
    b7721000-b7800000 ---p 00000000 00:00 0 
    b7807000-b7808000 rw-p 00000000 00:00 0 
    b781c000-b781f000 rw-p 00000000 00:00 0 
    bfcb5000-bfcca000 rw-p 00000000 00:00 0          [stack]
    Aborted
    Which means that it was stored in the array just fine, but couldn't be removed. Here is my destructor function:

    Code:
    void destroyArray(int arraysize, char** arrToDestroy) {
      int j;
      for(j = 0; j < arraysize; j++){
        printf("\nremoving \"%s\"\n", arrToDestroy[j]);
        free(arrToDestroy[j]);
      }
      printf("\nDESTROYING ARRAY AT MEM LOCATION: %p\n", (void *)arrToDestroy);
      free(arrToDestroy);
    }
    I should note that something shorter like "My cat Fritz" works just fine and is removed successfully.

  9. #24
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    Wait, I think I got it now - should have used strlen instead of sizeof to malloc enough memory for the name entered.

    Code:
        arrayOfNames[m] = malloc(strlen(name) + 1);
        strcpy(arrayOfNames[m], name);
    It still bugs me when the compiler thinks I'm trying to implicitly redefine both of those functions above, but it is working as expected this way.

  10. #25
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Are you including stdlib.h?

  11. #26
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    Yes, and strings.h

  12. #27
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    Well, here's the end result - after making a "removeName" function to compliment the "enterName" one. Can anyone get this to compile on their machine without warnings?

    Code:
    /**********
    * Author: TieFighter
    * Purpose: Make an array of strings,
    * be able to add and remove strings
    * dynamically, and destroy the array
    **********/
    
    #include <strings.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdbool.h>
    
    
    char ** createArray (int arraysize) {
      char **arrToCreate;
      arrToCreate = (char**)malloc(arraysize * sizeof(char *));
      printf("CREATED ARRAY AT MEM LOCATION: %p\n", (void *)arrToCreate);
      int i;
      for(i = 0; i < arraysize; i++) {
        arrToCreate[i] = NULL;
      }
      return arrToCreate;
    }
    
    bool enterName(char* name, int numSlots, char** arrayOfNames) {
      int m;
      for(m = 0; arrayOfNames[m] != NULL && m < numSlots; m++){
        printf("\nLooking through: %s\n", arrayOfNames[m]);
      }
      printf("\nm = %d\n", m);
      if(m >= numSlots) {
        printf("\nArray is full!  Stop trying to add stuff!  %s won't fit!\n", name);
        return false;
      } else {
        printf("\nFound a free spot to store: %s at [%d]\n", name, m);
        printf("strlen of name %s has value: %d", name, (strlen(name) + 1));
        arrayOfNames[m] = malloc(strlen(name) + 1);
        strcpy(arrayOfNames[m], name);
        return true;
      }
      return false; // If you made it this far there was probably no name entered.
    }
    
    bool removeName(char* name, int numSlots, char** arrayOfNames) {
      int m;
      for(m = 0; m < numSlots; m++) {
        if(arrayOfNames[m] != NULL) {
          if(strcmp(arrayOfNames[m], name) == 0) {
            printf("\nFreeing %s\n", name);
            free(arrayOfNames[m]);
            arrayOfNames[m] = NULL;
            return true;
          }
        }
      } 
      return false;
    }
    
    void destroyArray(int arraysize, char** arrToDestroy) {
      int j;
      for(j = 0; j < arraysize; j++){
        printf("\nremoving \"%s\"\n", arrToDestroy[j]);
        free(arrToDestroy[j]);
      }
      printf("\nDESTROYING ARRAY AT MEM LOCATION: %p\n", (void *)arrToDestroy);
      free(arrToDestroy);
    }
    
    int main(int argc, char **argv) {
      char **myArr;
      int arraysize = 4;
      myArr = createArray(arraysize);
      enterName("Jessica", arraysize, myArr);
      enterName("Gavin", arraysize, myArr);
      enterName("Vincent", arraysize, myArr);
      removeName("Gavin", arraysize, myArr);
      enterName("My cat Fritz\'s left front paw claw, which is really sharp", arraysize, myArr);
      enterName("Colin", arraysize, myArr);
      destroyArray(arraysize, myArr);
      exit(0);
    }

  13. #28
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, strings.h should be string.h.
    Remove the "return false" in enterName (because it will never be reached due to your if/else statement). Then it compiles without warnings (except for unused arguments).
    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.

  14. #29
    Registered User TieFighter's Avatar
    Join Date
    Feb 2010
    Location
    La Crosse, WI
    Posts
    32
    strings is changed to string - works perfectly, thank you.
    well, yeah it should never be reached, but if it ever is - then you know the name probably wasn't entered.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It cannot ever be reached because your if/else.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers to arrays
    By rakeshkool27 in forum C Programming
    Replies: 1
    Last Post: 01-24-2010, 07:28 AM
  2. 2d array of object pointers
    By Potterd64 in forum C++ Programming
    Replies: 17
    Last Post: 07-20-2006, 01:27 PM
  3. Pointers to Multidimensional Arrays
    By kidburla in forum C Programming
    Replies: 10
    Last Post: 10-29-2005, 10:45 PM
  4. returning 2D arrays
    By ... in forum C++ Programming
    Replies: 2
    Last Post: 09-02-2003, 12:28 PM
  5. Initialising 2D and 3D arrays
    By fry in forum C++ Programming
    Replies: 5
    Last Post: 08-01-2002, 04:34 AM