473,379 Members | 1,511 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,379 software developers and data experts.

function pointer question (weird behaviour)

BA
Hello all,

I need some help understand this ..

On Debian, using gcc :-

/* I have a structure defined as */
struct struImpl {
int unroll;
char *type;
void (*comp_func)(double *M, double *C);
void (*mvp_func)(double *C, double *X, double *Y);
}
/* Next I create an array of this struct as follows: */
struct struImpl Impls[] = {
{ 1, "CRS", compress2crs, mvp_crs },
{ 1, "CDS", compress2cds, mvp_cds }
};

/* also declare another function pointer */
void (*fptr)(double *, double *) = compress2crs;
printf(" Impls[0].comp_func = %x\n", Impls[0].comp_func); /* 1 */
printf(" compress2crs = %x\n", compress2crs); /* 2 */
printf(" fptr = %x\n", fptr); /* 3 */

now surprisingly,

/* 1 */ prints

Impls[0].comp_func = 8058d48
while /* 2 */ prints
compress2crs = 80497a8
and /* 3 */ prints
fptr = 80497a8

Any call made through fptr calls the correct function, while the
pointer in the structure does not seem to work.

Thank you,
Mar 18 '08 #1
1 1164
BA
On Mar 18, 11:43 am, BA <bivek.agra...@gmail.comwrote:
Hello all,

I need some help understand this ..

On Debian, using gcc :-

/* I have a structure defined as */
struct struImpl {
int unroll;
char *type;
void (*comp_func)(double *M, double *C);
void (*mvp_func)(double *C, double *X, double *Y);

}
Sorry, it was my mistake : I had it defined as
struct struImpl {
int unroll;
char *type;
void (*mvp_func)(double *C, double *X, double *Y);
void (*comp_func)(double *M, double *C);
}
>
/* Next I create an array of this struct as follows: */
struct struImpl Impls[] = {
{ 1, "CRS", compress2crs, mvp_crs },
{ 1, "CDS", compress2cds, mvp_cds }
};

/* also declare another function pointer */
void (*fptr)(double *, double *) = compress2crs;

printf(" Impls[0].comp_func = %x\n", Impls[0].comp_func); /* 1 */
printf(" compress2crs = %x\n", compress2crs); /* 2 */
printf(" fptr = %x\n", fptr); /* 3 */

now surprisingly,

/* 1 */ prints

Impls[0].comp_func = 8058d48
while /* 2 */ prints
compress2crs = 80497a8
and /* 3 */ prints
fptr = 80497a8

Any call made through fptr calls the correct function, while the
pointer in the structure does not seem to work.

Thank you,
Mar 18 '08 #2

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
10
by: Chris Mantoulidis | last post by:
I see some really weird output from this program (compiled with GCC 3.3.2 under Linux). #include <iostream> using namespace std; int main() { char *s; s = "test1"; cout << "s = " << s << "...
6
by: hongky gump | last post by:
#include <stdio.h> #include <string.h> char str; char append; .... sprintf(str, "%s%s", str, append); .... is it standard use?
11
by: risha | last post by:
hello, Need to know how the pointer can be initailzed to a particular location so that the value can be checked. void FlashWrite1(volatile unsigned short *addr1,volatile unsigned short...
1
by: Frederick Dean | last post by:
Hi, guys! Please look at the very simple code below: //---------------code begin------------------------ void f() { cout << "f function " << endl; } void (*pf)() = &f;
2
by: greg | last post by:
In my quest to eliminate C compiler warnings from Pyrex output, I've discovered some utterly bizarre behaviour from gcc 3.3. The following code: void g(struct foo *x) { } void f(void) {
29
by: Ravishankar S | last post by:
Dear C Experts, While prepating a content for a C course,I made section on function prototypes. Could you kindly provide me your comments on its correctness. Thank you ! Q12: What is the...
98
by: Micheal Smith | last post by:
I recently read an article containing numerous gripes about common C practices. One of them contained a gripe about the use of the sizeof operator as an argument to malloc calls. The supposed...
53
by: souporpower | last post by:
Hello All I am trying to activate a link using Jquery. Here is my code; <html> <head> <script type="text/javascript" src="../../resources/js/ jquery-1.2.6.js"</script> <script...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...

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.