473,383 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Waring about pointers

I am having some (compiler) problems with the following code. The
compiler complains of "warning: assignment makes pointer from integer
without a cast" at the lines I indicated. But it still compiles and
works as expected. So what is the proper way to write this.
void test(void){
printk("test\n");
}

/* In main */
register_isr(29,test);

/* In isr.c */
int register_isr(unsigned int vector, void (*isr)(void)){
volatile unsigned long *vaddr; /* Vector Address */
vaddr = (volatile unsigned long) EVT_BASE + (vector * 4); /*
warings here */
*vaddr = isr; /* and here */
return 0;
}

So whats wrong with this?

-kyle

Feb 17 '06 #1
5 1507
"kyle.tk" <ky*****@gmail.com> writes:
I am having some (compiler) problems with the following code. The
compiler complains of "warning: assignment makes pointer from integer
without a cast" at the lines I indicated. But it still compiles and
works as expected. So what is the proper way to write this.
void test(void){
printk("test\n");
}

/* In main */
register_isr(29,test);

/* In isr.c */
int register_isr(unsigned int vector, void (*isr)(void)){
volatile unsigned long *vaddr; /* Vector Address */
vaddr = (volatile unsigned long) EVT_BASE + (vector * 4); /*
warings here */
*vaddr = isr; /* and here */
return 0;
}

So whats wrong with this?


vaddr is a pointer (of type volatile unsigned long*). The value you
assign to it is an integer (of type volatile unsigned long).

In the second assignment, "*vaddr = isr;", isr is a pointer to a
function; *vaddr is an unsigned long.

You're obviously doing some tricky low-lever stuff here. Without more
information (we don't know what EVT_BASE is, for example), it's hard
to tell what you *should* be doing.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Feb 17 '06 #2
Hi,

kyle.tk wrote:
/* In isr.c */
int register_isr(unsigned int vector, void (*isr)(void)){
volatile unsigned long *vaddr; /* Vector Address */
vaddr = (volatile unsigned long) EVT_BASE + (vector * 4); /*
warings here */
1) There could be 2 errors, one logical and one cast. The statement
could be one of the following (Assuming that the isr address should be
located at 29*4 bytes from EVT_BASE)..
- vaddr = (volatile unsigned long*)EVT_BASE + vector ;
- vaddr = (volatile unsigned long*)(EVT_BASE + (vector * 4));

2) Do you think that the volatile declaration is necessary in this
context? I dont see the cause for that memory location to be volatile.
*vaddr = isr; /* and here */
return 0;
3) The above statement also has the cast issues. Either declare the
"vaddr" to be a "function pointer type" or type-cast the "isr" to
"vaddr" type. This could eliminate the warnings.
So whats wrong with this?

-kyle


Feb 17 '06 #3
Hi,

kyle.tk wrote:
/* In isr.c */
int register_isr(unsigned int vector, void (*isr)(void)){
volatile unsigned long *vaddr; /* Vector Address */
vaddr = (volatile unsigned long) EVT_BASE + (vector * 4); /*
warings here */
1) There could be 2 errors, one logical and one cast. The statement
could be one of the following (Assuming that the isr address should be
located at 29*4 bytes from EVT_BASE)..
- vaddr = (volatile unsigned long*)EVT_BASE + vector ;
- vaddr = (volatile unsigned long*)(EVT_BASE + (vector * 4));

2) Do you think that the volatile declaration is necessary in this
context? I dont see the cause for that memory location to be volatile.
*vaddr = isr; /* and here */
return 0;
3) The above statement also has the cast issues. Either declare the
"vaddr" to be a "function pointer type" or type-cast the "isr" to
"vaddr" type. This could eliminate the warnings.
So whats wrong with this?

-kyle


Feb 17 '06 #4
kyle.tk wrote:
I am having some (compiler) problems with the following code. The
compiler complains of "warning: assignment makes pointer from integer
without a cast" at the lines I indicated. But it still compiles and
works as expected. So what is the proper way to write this.

void test(void){
printk("test\n");
}

/* In main */
register_isr(29,test);

/* In isr.c */
int register_isr(unsigned int vector, void (*isr)(void)){
volatile unsigned long *vaddr; /* Vector Address */
Here you declare vaddr as a pointer.
vaddr = (volatile unsigned long) EVT_BASE + (vector * 4); /*
warings here */
Here you assign an integer to it. Integers and pointers are *not* the
same thing, so the compiler is quite correct to complain.

Not knowing the definition of EVT_BASE is it impossible to say what the
correct solution to it is. However, I would guess that what you want to
do is loose the cast and ensure that EVT_BASE is of the correct type.
*vaddr = isr; /* and here */
Here you try to assign a pointer to a function to the location that
vaddr points to but vaddr is defined as pointing to an integer
(specifically an unsigned long) not a function pointer.
return 0;
}

So whats wrong with this?


The problem seems to be you thinking pointers and integers are the same
thing. They most definitely are *not* the same.

If you post a compilable minimal example (which would at least include
the definition of EVT_BASE and preferable some sample code calling this
function) together with what you think you are trying to do we can help
better.

I'm guessing that EVT_BASE is the start of an interrupt vector table
(which is OT here) and this function is trying to assign functions to
specific location.

I think what you actually want is something like the following untested
code:
typedef void (*isr_t)(void);
#define EVT ((volatile isr_t*)0x1234)

void register_isr(unsigned int vector, isr_t isr)
{
EVT[vector] = isr;
}

Another possible definition for EVT is:
extern volatile isr_t EVT[NO_OF_ISRS];
Then you can possibly use some linker magic (which is OT here) to place
EVT at the correct location.

Note the use of the typedef for the function pointer to make writing the
rest of it simpler. Also note that apart from casting the address of the
interrupt vector table to a pointer I've got rid of the need to cast
anything. In general, casts are bad. However, if I'm correct in my
assumptions of what you are trying to do, this is an instance where you
need to convert an integer to a pointer, something which gives an
implementation defined result at best and undefined behaviour at worst.

Please note that you are skirting the edges of topicality. How your
interrupt vector table works in not topical, but given that it is an
array of pointers to functions at an address that you know we can
discuss ways you can write to it.
--
Flash Gordon
Living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro -
http://clc-wiki.net/wiki/Intro_to_clc
Feb 17 '06 #5
Q
to follow up on what Keith said, it is hard to give accurate advice not
fully understanding all of the variables

however, in the statements where u have:
vaddr = (volatile unsigned long) EVT_BASE + (vector * 4);
when assigning a value to a pointer, it expects a pointer (address) in
return, UNLESS you dereference it first (i.e. *vaddr = (. . .))

since your placing a value in the space pointed to by vaddr, try using
*vaddr= instead

as for the next line where u wrote:
*vaddr = isr;
"isr" runs and returns no value (void) to a variable that expects a
"volatile unsigned long". it should be safe to call isr as follows:
isr(); or (*isr) ();


without using the *vaddr=

Feb 17 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
0
by: summer911 | last post by:
Hi I tried the code below ( xmlParseFile) on my xml file ( see below ) but this gives me a warning message!(OS :redhat9.0) The warning message is :I/O waring :failed to load external extity "" ...
3
by: ozbear | last post by:
This is probably an obvious question. I know that pointer comparisons are only defined if the two pointers point somewhere "into" the storage allocated to the same object, or if they are NULL,...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
12
by: Lance | last post by:
VB.NET (v2003) does not support pointers, right? Assuming that this is true, are there any plans to support pointers in the future? Forgive my ignorance, but if C# supports pointers and C# and...
14
by: Alf P. Steinbach | last post by:
Not yet perfect, but: http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01.pdf http://home.no.net/dubjai/win32cpptut/special/pointers/ch_01_examples.zip To access the table of...
6
by: Murali Yeddanapudi | last post by:
Hi The C code (not C++) below (test.c) generates a warning: #include <stdlib.h> int main() { const char** a = (const char**) malloc(1*sizeof(const char*)); a = "boo"; free(a);
25
by: J Caesar | last post by:
In C you can compare two pointers, p<q, as long as they come from the same array or the same malloc()ated block. Otherwise you can't. What I'd like to do is write a function int comparable(void...
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.