Solved

"assignment makes pointer from integer without a cast" problem

Posted on 2011-03-08
17
820 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:cstsang
  • 7
  • 7
  • 3
17 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 35067969
Which line number (and which file) does the error refer to ?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35068038
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
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35068041
Sorry, this was a little bit mis-formatted :o(
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 7

Author Comment

by:cstsang
ID: 35068301
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.

0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35068359
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
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35068508
>> 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
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35068512
>> 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.
0
 
LVL 7

Author Comment

by:cstsang
ID: 35075453
is it my makefile have some problem?
In fact, b.c is 1 a file in  the huge lib.(*.a).
0
 
LVL 7

Author Comment

by:cstsang
ID: 35076328
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

0
 
LVL 7

Author Comment

by:cstsang
ID: 35079454
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.
0
 
LVL 7

Author Comment

by:cstsang
ID: 35079967
If I combine these 2 file into a file,and then compile it, it works fine.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35080024
>> 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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35080028
>> 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 ;)
0
 
LVL 7

Author Comment

by:cstsang
ID: 35080374
Can you create a *.h file for me?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 35080432
For the b.c file you posted earlier, it would look something like below. Make sure to #include "b.h" in both b.c and test.c !
#ifndef B_H
#define B_H

void sw_first(int argc, char *argv[]);

int c_switch(char c);    /* return 1 if c is included else return 0 */

/*
      *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) ;


#endif /* B_H */

Open in new window

0
 
LVL 7

Author Closing Comment

by:cstsang
ID: 35080472
is it possible to create a header for a lib?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 35080892
>> is it possible to create a header for a lib?

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

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question