try changing "bool" to "_Bool" and use -std=c99 with gcc
try changing "bool" to "_Bool" and use -std=c99 with gcc
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.Originally Posted by Elysia
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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.
...and the compiler saysCode://arrayOfNames[m] = malloc(sizeof(name) + 1); memmove(arrayOfNames[m], name, (sizeof(name) + 1) );
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?Code:In function ‘enterName’: 36: warning: incompatible implicit declaration of built-in function ‘memmove’
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.
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:
and here's how I'm trying to enter the names into the array -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
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.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); }
Last edited by TieFighter; 03-17-2010 at 11:08 PM.
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
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:
Which means that it was stored in the array just fine, but couldn't be removed. Here is my destructor function: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
I should note that something shorter like "My cat Fritz" works just fine and is removed successfully.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); }
Wait, I think I got it now - should have used strlen instead of sizeof to malloc enough memory for the name entered.
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.Code:arrayOfNames[m] = malloc(strlen(name) + 1); strcpy(arrayOfNames[m], name);
Are you including stdlib.h?
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); }
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.