* warning: assignment makes integer from pointer without a cast
@ 2003-05-06 13:57 Martin Buchan
2003-05-06 19:44 ` Glynn Clements
0 siblings, 1 reply; 4+ messages in thread
From: Martin Buchan @ 2003-05-06 13:57 UTC (permalink / raw)
To: linux-c-programming
Hi,
I am trying to populate an array with strings which i have
extracted from another string using strtok. I have managed to split
up the string into its constituent parts but i cant seem to populate
the array.
The error and line where it's failing on is at the bottom of the
post, what follows is some background info if it may help.
One problem is that it is unknown how many elements will
be in the new array. (I.e. the original string length changes all
the time) so i have used malloc to allocate some memory for it. Here
is some of my code - the malloc code for the new array is in the
item_response() function at the end.
struct menuentry
{
char *appsection;
char *appsubmenu;
int numargs; /* used to get size of array */
};
typedef struct menuentry menuentry;
I have an array of structs (also of unknown size at compile time) which
i create using malloc also and then go off and populate the structs
with what i need to populate it with. This all works fine.
menuentry *mePtr = malloc( n * sizeof (menuentry) ); /* Create space for array of structs */
I then assign a value to menuentry[i].numargs with the following code.
/* Get number of arguments */
numAppArgs = strdup(key); /* key is a pointer to the string i want to slpit */
token = strtok( numAppArgs, sep );
while( token != NULL )
{
n++;
token = strtok( NULL, sep );
}
mePtr[i].numargs = n; /* amount of elements i split the string into */
In another function, I pass the address of what this pointer points
to, to a GTK+ method like so (The method expects a pointer)
gtk_object_set_data(GTK_OBJECT (menu_items), "numargs_key", &mePtr[i].numargs);
I then call another function passing it the menu_items object which
has a bunch of data attached to it attached as key-value pairs where
the value is a pointer to some data.
Here is the function.
static void item_response( GtkObject *passme )
{ /* Execute Apps */
char *appArgs, *tmpappArgs, *token, *appCommand;
int pid, i = 0;
char sep[] = " ";
int *numArgs = gtk_object_get_data(passme, "numargs_key"); /* Get number of arguments */
char *nArgsPtr = malloc( *numArgs * sizeof (char) ); /* Create array of size numArgs */
tmpappArgs = gtk_object_get_data(passme, "args_key");
printf("num args: %d\n", *numArgs); /* Prints out correctly */
appArgs = strdup(tmpappArgs);
printf("args: %s\n", appArgs ); /* Prints out correctly */
token = strtok( appArgs, sep );
while( token != NULL )
{
nArgsPtr[i] = strdup(token); /* Fails on this line - 623 */
printf( " %s I: %d\n", nArgsPtr[i], i ); /* Fails on this line - 624 */
i++;
token = strtok( NULL, sep );
}
appCommand = gtk_object_get_data(passme, "command_key");
/* snipped fork() checking etc */
execvp(appCommand, (gpointer) nArgsPtr); /* This is what i am trying to achieve */
/* snipped */
}
Here is what i get when i compile using gcc-2.95.2 on sparc solaris
gcc -Wall menu-0.5.c -o menu-0.5 gtk-config --cflags gtk-config \
--libs -I/usr/local/include/libxml2 -Wall -lxml2
menu-0.5.c: In function item_response':
menu-0.5.c:623: warning: assignment makes integer from pointer without a cast
menu-0.5.c:624: warning: format argument is not a pointer (arg 2)
I dont understand why it says i am assigning integer from pointer
when i have declared nArgsPtr as a char ( char *nArgsPtr)
Does anyone know what I am doing wrong here?
Thanks
Martin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: warning: assignment makes integer from pointer without a cast
2003-05-06 13:57 warning: assignment makes integer from pointer without a cast Martin Buchan
@ 2003-05-06 19:44 ` Glynn Clements
0 siblings, 0 replies; 4+ messages in thread
From: Glynn Clements @ 2003-05-06 19:44 UTC (permalink / raw)
To: Martin Buchan; +Cc: linux-c-programming
Martin Buchan wrote:
> I am trying to populate an array with strings which i have
> extracted from another string using strtok. I have managed to split
> up the string into its constituent parts but i cant seem to populate
> the array.
>
> The error and line where it's failing on is at the bottom of the
> post, what follows is some background info if it may help.
> static void item_response( GtkObject *passme )
> { /* Execute Apps */
> char *appArgs, *tmpappArgs, *token, *appCommand;
> int pid, i = 0;
> char sep[] = " ";
>
> int *numArgs = gtk_object_get_data(passme, "numargs_key"); /* Get number of arguments */
> char *nArgsPtr = malloc( *numArgs * sizeof (char) ); /* Create array of size numArgs */
This should be an array of pointers:
char **nArgsPtr = malloc( *numArgs * sizeof (char *) ); /* Create array of size numArgs */
> nArgsPtr[i] = strdup(token); /* Fails on this line - 623 */
> printf( " %s I: %d\n", nArgsPtr[i], i ); /* Fails on this line - 624 */
> Here is what i get when i compile using gcc-2.95.2 on sparc solaris
>
> gcc -Wall menu-0.5.c -o menu-0.5 gtk-config --cflags gtk-config \
> --libs -I/usr/local/include/libxml2 -Wall -lxml2
>
> menu-0.5.c: In function item_response':
> menu-0.5.c:623: warning: assignment makes integer from pointer without a cast
> menu-0.5.c:624: warning: format argument is not a pointer (arg 2)
>
>
> I dont understand why it says i am assigning integer from pointer
> when i have declared nArgsPtr as a char ( char *nArgsPtr)
>
> Does anyone know what I am doing wrong here?
nArgsPtr has type "char *", so nArgsPtr[i] has type "char", which is
an integral type. If you declare nArgsPtr with type "char **",
nArgsPtr[i] has type "char *".
You also have to fix the size; the array consists of *numArgs
pointers, not *numArgs characters.
--
Glynn Clements <glynn.clements@virgin.net>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: warning: assignment makes integer from pointer without a cast
@ 2003-05-07 15:44 Martin Buchan
2003-05-08 19:47 ` <time.h>: gmtime keeps giving the same time J.
0 siblings, 1 reply; 4+ messages in thread
From: Martin Buchan @ 2003-05-07 15:44 UTC (permalink / raw)
To: linux-c-programming
Hi,
Thanks to the list for the pointers.
On Tue, May 06, 2003 at 08:44:16PM +0100, Glynn Clements wrote:
> Martin Buchan wrote:
>
> > I am trying to populate an array with strings which i have
> > extracted from another string using strtok. I have managed to split
> > up the string into its constituent parts but i cant seem to populate
> > the array.
>
> > static void item_response( GtkObject *passme )
> > { /* Execute Apps */
> > char *appArgs, *tmpappArgs, *token, *appCommand;
> > int pid, i = 0;
> > char sep[] = " ";
> >
> > int *numArgs = gtk_object_get_data(passme, "numargs_key"); /* Get number of arguments */
> > char *nArgsPtr = malloc( *numArgs * sizeof (char) ); /* Create array of size numArgs */
>
> This should be an array of pointers:
>
> char **nArgsPtr = malloc( *numArgs * sizeof (char *) ); /* Create array of size numArgs */
>
I've ended up doing it a different way altogether as even with the
suggestions on here, i still couldn't get it to compile/run. (Even
after i had sorted *numArgs)
What i have instead which is cleaner i think is.
static void parse(buf, args)
char * buf;
char **args;
{
while (*buf != NULL) {
/* Strip whitespace. Use nulls, so that the previous argument
* is terminated automatically. */
while ((*buf == ' ') || (*buf == '\t')) // || is OR
*buf++ = NULL;
/* Save the argument. */
*args++ = buf;
/* Skip over the argument. */
while ((*buf != NULL) && (*buf != ' ') && (*buf != '\t'))
buf++;
}
*args = NULL;
}
static void item_response( GtkObject *passme )
{ /* Execute Apps */
char *buf;
char *args[64];
buf = gtk_object_get_data(passme, "command_key"); /* assign my command and args to buf */
parse(buf, args);
execute(args); /* Here i call the execvp stuff with the properly sized array */
}
Thanks again
Martin
^ permalink raw reply [flat|nested] 4+ messages in thread
* <time.h>: gmtime keeps giving the same time ....
2003-05-07 15:44 warning: assignment makes integer from pointer without a cast Martin Buchan
@ 2003-05-08 19:47 ` J.
0 siblings, 0 replies; 4+ messages in thread
From: J. @ 2003-05-08 19:47 UTC (permalink / raw)
To: linux-c-programming
Hello,
Output from the date `command` in my terminal:
Thu May 8 21:42:28 CEST 2003
Output from my `problem'_program.c
19
#include <stdio.h>
#include <time.h>
int main(void) {
struct tm *tm_ptr;
time_t the_time;
(void)time(&the_time);
tm_ptr = gmtime(&the_time);
printf("tm_ptr->tm_hour = %d\n", tm_ptr->tm_hour);
return 0;
}
No matter what I do, this C - code keeps giving me as output 19.
Is my system messed up or my head ?
Any thoughts are more than welcome
Thnkx....
J.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2003-05-08 19:47 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-07 15:44 warning: assignment makes integer from pointer without a cast Martin Buchan
2003-05-08 19:47 ` <time.h>: gmtime keeps giving the same time J.
-- strict thread matches above, loose matches on Subject: below --
2003-05-06 13:57 warning: assignment makes integer from pointer without a cast Martin Buchan
2003-05-06 19:44 ` Glynn Clements
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).