5

I am trying to initialize an array of structs. the structs have function pointers in them and are defined as such:

typedef struct{
      char str[512];
      char *fptr;
} infosection;

then I try to make an array:

infosection section[] = {
      ("Software", *SoftwarePtr),
      ("Hardware", *HardwarePtr),
 }

I defined the function pointers using a simple tutorial. Function Software returns an int

int (*SoftwarePtr)()=NULL;
SoftwarePtr = &Software;

My question is about the warnings I get upon compiling.

Initialization makes integer from pointer without a cast

The warning references the lines in the section array.

So I have two doubts:

  1. Am I misusing the */&/neither for the pointer?I have tried combinations of each and i still seem to get the same warning.I am aware of the meaning of each, just unsure how they apply in this particular instance.
  2. Can I declare an instance of the infosection struct in an array as i do here?I have seen many examples where people declare their array of struct in a for loop, However my final product requires a long list of structs to be contained in that array.I would like to make my code portable such that people can add structs ( strings which correspond with functions to point to) in an easy list-like fashion as seen about. Is this the only way to declare an instance of the array

      infosection section[];
      section[0].str="software";
      section[0].fptr=SoftwarePtr;
    

Just to clarify, i have done quite a bit of research on structs, array of structs, and function pointers. It just seems that the combination of the 3 is causing trouble.

5
  • I thought function pointers were void *(). Commented Jul 26, 2013 at 17:56
  • @Jim I read here: newty.de/fpt/fpt.html#defi that function pointer's need to be declared with the return type of the function they point to... Commented Jul 26, 2013 at 18:00
  • char *fptr; This is not a function pointer and conversions between char* and a function pointer are not well-defined Commented May 26, 2015 at 14:45
  • Anyway, a better question might be: which crap compiler allowed all of these errors to slip through? Please don't tell me you are using Turbo C... Commented May 26, 2015 at 14:51
  • That was GCC my friend Commented May 27, 2015 at 12:31

3 Answers 3

7

One

One mistake I an find, in structure declaration of function pointer is incorrect:

typedef struct{
      char str[512];
      int (*fptr)();  // not char* fptr
} infosection;

Two

declaration:

infosection section[] = {
      ("Software", *SoftwarePtr),
      ("Hardware", *HardwarePtr),
 }

Should be:

infosection section[] = {
      {"Software", SoftwarePtr},
      {"Hardware", HardwarePtr}
 }

Remove *. and replace inner ( ) with { }.

three:

infosection section[];
section[0].str="software"; // compiler error
section[0].fptr=SoftwarePtr;

Is wrong you can't assign string in this way. you need to use strcpy() as follows;

strcpy(section[0].str, "software");
Sign up to request clarification or add additional context in comments.

1 Comment

There is nothing wrong with the trailing comma: some coding styles even favour it as it makes each row consistent. Array initialization lists have always allowed to have trailing commas on the last row.
2

The struct member is currently a char*, change to the correct pointer type for your function signature.

typedef struct{
  char str[512];
  int (*fptr)();
} infosection;

* dereference the pointer, read *foo as "what foo points at". You want to set the struct member to the actual pointer, also use bracets instead of paranthesis.

infosection section[] = {
  {"Software", SoftwarePtr},
  {"Hardware", HardwarePtr}
}

Comments

1

been a while since i was playin around with pointers but maybe a constructor would help?

struct infosection{
      char str[512];
      char *fptr;
      infosection(char* strN, char* fptrN): str(strN), fptr(fptrN) {}
};

infosection[] isxn = { new infosection(&str1,fptr1), 
               new infosection(&str2,fptr2)},
               ... };

my apologies...where c stops and c++ begins has become blurred in my mind, i have seen a workaround for c that looks like this (but not exactly, im sure some of the pointer stuff is mixed up but this was the idea)

struct infosection{
  char str[512];
  char *fptr;
};

infosection* myIS (char *strN, char *fptrN) {
  infosection i = new infosection();
  i.str = &strN;
  i.fptr = fptrN;
  return *i;
}

//infosection* newInfoSection = myIS(myStrPtr,myFptr);

1 Comment

Constuctors are a part of C++ though, and wont work in this senario =)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.