Link to home
Start Free TrialLog in
Avatar of KNVB HK
KNVB HKFlag for Hong Kong

asked on

"assignment makes pointer from integer without a cast" problem

When I compile my program(test.c) I got the following error:
"assignment makes pointer from integer without a cast"

here is my code:



 
test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main (int argc, char *argv[])
{
	char *a[4];
	char b[255];
	char *c;
	int argn,z[4];

	sw_first(argc,argv);
	for (argn=0;argn<4;argn++)
	{
		a[argn]=c_rdarg(0,&z[argn]);
	}
}

Open in new window

 
b.c:
#include <stdio.h>
#include <stdlib.h>

#define max_S 52
#define max_R 50
static int isw=0, iarg=0, f=1;
static char sw[max_S];
static char * arg[max_R];
static short ct_arg[max_R];
static char * temp;
void sw_first(int argc, char *argv[])
{
 int i,k;
 char c;
 char * xx;
 if (f){
   f=0;        /* flag sw and arg saved */
   for (i=1; i<argc; i++){              /* i=1 .. skip program name */
     xx=argv[i];
     if ((xx[0]=='-') && (xx[1]!='\0') && ((xx[1]>'9') || (xx[1]<'0')))
       while (c=*++xx)
	sw[isw++]=c;                    /* saved to switch array */
     else{
       arg[iarg]=xx;
       for (k=0; (c=*xx++); k++);       /* count no of chars in arg */
       ct_arg[iarg++]=k;
     }
   }
 }
}
int c_switch(char c)    /* return 1 if c is included else return 0 */
{
static int i=0;    /* so i can be used again as start next search */
int j,k;
j=0;
if (f)
  printf("You should call sw_first once before calling c_switch !!\n");
else{
  for (k=0;k<isw;k++){
   if (c==sw[i]){
     j=1;
     break;
   }
   i++;
   if (i>=isw)
     i=0;
  }
}
return j;
}
/*
      *c_rdarg = pointer to arg
      order    > 0 then return the (order)th arg
	       = 0 sequential scan of arg until the last
      *ct      = return no of chars in arg excluding NULL
*/
char* c_rdarg(int order,int  *ct) 
{
static int i=0;
int k;
if (f){
 printf("You should call sw_first once before using c_rdarg !!\n");
 ct=0;
 return '\0';
}
if ((order>0) && (order<iarg)){
  --order;
  *ct=ct_arg[order];
  return arg[order];
}else if (iarg>i){
  k=i++;
  *ct=ct_arg[k];
  temp=arg[k];
  return arg[k];
}else
  *ct=0;
  return '\0';
}

Open in new window


Can you tell what is "assignment makes pointer from integer without a cast"?
Where can I found  the error occur in my code?
And how to solve the problem?
I used 1 day time to deal with the code, I don't know how to solve it?
And I am green in cc, does any web site/book should I take a look?

thank you very much
Avatar of Infinity08
Infinity08
Flag of Belgium image

Which line number (and which file) does the error refer to ?
Hi cstsang,

I agree with Infinity08, telling us in which file/line the error is generated is very helpful.

BTW, I think the two return '\0'; in
c_rdarg

Open in new window

may be the problem, since c_rdarg is declared to return a char* but is returning a char - maybe you intended something an empty, zero terminated string like return "";

ZOPPO
Sorry, this was a little bit mis-formatted :o(
Avatar of KNVB HK

ASKER

In the error message is came from the following statement in  test.c.

a[argn]=c_rdarg(0,&z[argn]);

When I compile b.c using the following command:

 cc -c b.c
It return no error.
When I compile all *.o file, it prompt the error

cc -c b.c -o b.o
cc -c test.c -o test.o

cc b.o test.o test.c -o test

by the way, I will try you suggestion.

Hm - maybe you somewhere declared 'c_rdarg' different.

Generally it should be ok since implementation of 'c_rdarg' returns a 'char*' and 'a[argn]' is a 'char*'.

But, something is missing in the code I guess since in 'test.c' there's no declaration of 'c_rdarg' which has to be there to get it compiled. Is the error message you posted above the only one you recieve? If not could you post all error messages?

ZOPPO
>> In the error message is came from the following statement in  test.c.

The problem is that in test.c, you don't have the prototype for the =c_rdarg function.

You need to #include "b.h" in test.c. And if you don't have a b.h, then make one that has the prototype of every function defined in b.c
>> But, something is missing in the code I guess since in 'test.c' there's no declaration of 'c_rdarg' which has to be there to get it compiled. Is the error message you posted above the only one you recieve?

The error message is exactly because of the missing function prototype.

In the absence of a declaration, the compiler will assume a function that takes no parameters, and returns int.
Avatar of KNVB HK

ASKER

is it my makefile have some problem?
In fact, b.c is 1 a file in  the huge lib.(*.a).
Avatar of KNVB HK

ASKER

I found a compiler option -w to suppress the warning.
In fact, the executable file is produced by compiler even it prompt an error message.
However, I changed the program test.c as the following, it prompt an "Segmentation fault" error.
Can you tell me why?
thank you very much

 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main (int argc, char *argv[])
{
	char *a[4];
	char b[255];
	char *c;
	int argn,z[4];

	sw_first(argc,argv);
	for (argn=0;argn<4;argn++)
	{
		a[argn]=c_rdarg(0,&z[argn]);
		printf("%s\n",a[argn]);
	}
}

Open in new window

Avatar of KNVB HK

ASKER

In fact, I am migrating a program from AIX to redhat linux, does any compiler option can solve the problem?

I tried to compile the program in AIX, it works.
Avatar of KNVB HK

ASKER

If I combine these 2 file into a file,and then compile it, it works fine.
>> is it my makefile have some problem?

No. The problem, as I said, is that you haven't declared the c_rdarg function in the test.c file. If you don't believe me, just add this line somewhere at the top of test.c and rebuild it :

        char* c_rdarg(int order,int  *ct);

Things should work fine then.

To solve this properly though, you should add this line at the top of test.c :

        #include "b.h"

and make sure that there is such a header file that contains all function declarations for functions defined in b.c



And btw :

>> I found a compiler option -w to suppress the warning.

That's a bad idea ;) Warnings usually have a pretty good reason. And in this case, it probably told you exactly what the cause of the segmentation fault is.
>> If I combine these 2 file into a file,and then compile it, it works fine.

Yes, it confirms what I said above, but it's not the proper way of resolving this. Please check my previous post ;)
Avatar of KNVB HK

ASKER

Can you create a *.h file for me?
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KNVB HK

ASKER

is it possible to create a header for a lib?
>> is it possible to create a header for a lib?

Any decent library should already come with the proper header files.