I just don't understand what I'm really doing with the malloc double pointer char initialization! Aggghh..
The error is segment fault. Any other pointers for what's wrong with my code would be greatly appreciated! Merry Christmas!
I just don't understand what I'm really doing with the malloc double pointer char initialization! Aggghh..
The error is segment fault. Any other pointers for what's wrong with my code would be greatly appreciated! Merry Christmas!
Last edited by Kleid-0; 12-19-2004 at 11:27 PM. Reason: I forgot to say my error message! And I forgot to say Merry Christmas!
The malloc() thing allocates room for an array of pointers, but the pointers of this array are not initialized. If you want to use them, you also must allocate some memory block and record its address in the array of pointers. You code must be completed.Originally Posted by Kleid-0
Pointers are the tricky point of the C language. I suggest you work on it mre deeply before going further. It must be crystal clear in your mind.
Emmanuel Delahaye
"C is a sharp tool"
I'm glad you missed me. I was a little too busy. I'll try to come here a little more often...Originally Posted by Thantos
See ya!
Emmanuel Delahaye
"C is a sharp tool"
Originally Posted by Emmanuel Delaha
Well I understand pointer basics, it's just the double whammies confuse me because it makes sense to me, just not to the compiler! Agh..
Would you please copy and paste your code (with comments if you want) so that we can get your original code for chack and test, and not some by-hand-and-full-of-typos copy...Originally Posted by Kleid-0
The 'image' trick is just boring (and expensive in terms of disk space)
Emmanuel Delahaye
"C is a sharp tool"
I have no typos for one (because I am perfect!). I thought you guys would like the anti-aliased color coating! & plus some lucky HTML folks might see that I'm using GIF, remember to use GIF on images with not that many colors! I could kind of understand on disk space, but now'a days we've got like 80 GB hd's. Plus it's good typing practice! And it's not a by-hand copy, it's completely electronic! I don't understand! I know a pointer wiz can do this, BE the compiler, I have faith in you Emmanuel, you are the compiler soldier! I'm here for you every step!Originally Posted by Emmanuel Delaha
I don't doubt you are perfect, but I know I'm not. This is why I expect your code to be in a text mode because I am certainely not going to retype it. About the colors, my editor is big enough to colorize the C source according to my standards.Originally Posted by Kleid-0
Emmanuel Delahaye
"C is a sharp tool"
Now this thread looks ugly! :(
Code:#define THE_START "wget http://www.fffin.com/ikonit/ff6/enemies/" #define THE_END ".gif" int main(int args, char **argc) { char **Images = (char**)malloc(512); (*Images) = (char*)malloc(255); (*Images+1) = (char*)malloc(255); strcpy((*Images), "guard"); strcpy((*Images)+1, "lobo"); char *szExec = (char*)malloc(255); int i; for(i=0; i<sizeof(Images); i++) { strcpy(szExec, THE_START); strcat(szExec, Images[i]); strcat(szExec, THE_END); //system(szExec); printf("%s", szExec); } }
The first thing I notice is that you are casting the return value of malloc(). This is a big no no. Also where are you #include's? If you are getting errors about the void* then you are compiling it as c++ and should be using new and delete instead. Speaking of delete where are you calling free to deallocate the memory you malloc'd?
Here's a tip - copy/paste your code from your text editor into a set of [code][/code] tags.
sure it looks pretty, but it is absolutely USELESS to everyone else who want's to copy/paste your code for experiment or comment.
> char **Images = (char**)malloc(512);
1. Remove the cast (see the FAQ)
2. Add #include <stdlib.h>
3. Use a C compiler (not a C++ compiler which your code seems to need)
4. the size is totally bogus
> for(i=0; i<sizeof(Images); i++) {Code:char **Images = malloc( 512 * sizeof *Images );
What exactly are you hoping to achieve with this?
If it's an attempt to count the number of strings, then it fails miserably.
Try that, and ask for details if you don't understand.Originally Posted by Kleid-0
It produces:PHP Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define THE_START "wget http://www.fffin.com/ikonit/ff6/enemies/"
#define THE_END ".gif"
int main ()
{
size_t nb = 2;
char **Images = malloc (nb * sizeof Images);
if (Images != NULL)
{
Images[0] = malloc (64);
if (Images[0] != NULL)
{
Images[1] = malloc (64);
if (Images[1] != NULL)
{
strcpy ((Images[0]), "guard");
strcpy (Images[1], "lobo");
{
char *szExec = malloc (255);
size_t i;
for (i = 0; i < nb; i++)
{
strcpy (szExec, THE_START);
strcat (szExec, Images[i]);
strcat (szExec, THE_END);
/* system(szExec); */
printf ("%s\n", szExec);
}
}
free (Images[1]), Images[1] = NULL;
}
free (Images[0]), Images[0] = NULL;
}
free (Images), Images = NULL;
}
/* Dev-C++ trick */
system ("pause");
return 0;
}
Note that if the only purpose of the program is to insert these strings into the url, it could be done with farless malloc() things. A simple array of strings would suffice...Code:wget http://www.fffin.com/ikonit/ff6/enemies/guard.gif wget http://www.fffin.com/ikonit/ff6/enemies/lobo.gif Appuyez sur une touche pour continuer . . .
PHP Code:
static char const *Images[] =
{
"guard",
"lobo"
};
Last edited by Emmanuel Delaha; 12-20-2004 at 02:54 AM.
Emmanuel Delahaye
"C is a sharp tool"
Man, is that all the code is supposed to do?
It's like a 5-second job in a shell script.
Code:files="guard lobo" for i in $files ; do wget http://www.fffin.com/ikonit/ff6/enemies/$i.gif done
>Man, is that all the code is supposed to do?
>It's like a 5-second job in a shell script.
Heh. Well there is the whole learning experience too ya know.