Solved

"assignment makes pointer from integer without a cast" problem

Posted on 2011-03-08
17
807 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
Comment Utility
Which line number (and which file) does the error refer to ?
0
 
LVL 30

Expert Comment

by:Zoppo
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
Sorry, this was a little bit mis-formatted :o(
0
 
LVL 7

Author Comment

by:cstsang
Comment Utility
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 30

Expert Comment

by:Zoppo
Comment Utility
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
Comment Utility
>> 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
Comment Utility
>> 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
Comment Utility
is it my makefile have some problem?
In fact, b.c is 1 a file in  the huge lib.(*.a).
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 7

Author Comment

by:cstsang
Comment Utility
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
Comment Utility
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
Comment Utility
If I combine these 2 file into a file,and then compile it, it works fine.
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
Comment Utility
>> 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
Comment Utility
Can you create a *.h file for me?
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
Comment Utility
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
Comment Utility
is it possible to create a header for a lib?
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> is it possible to create a header for a lib?

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
In C, why can't I shift a 64-bit integer more than 32 bits? 13 192
Need example 5 100
Problem to ASCII 1 148
Line meaning 9 75
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now