Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 days ago.
The community is reviewing whether to reopen this question as of 4 days ago.
Improve this question
I'm learning C language and I bumped into a line that is like the following one:
void myControl(const myInput*, myOutput*, myRuntime*, const myConfig*);
what does the * symbol mean after the variables?
I looked for 'C language * meaning' in the internet but I always find * as the symbol for the pointers that is placed before and not after a variable name.
In the line:
void myControl(const myInput*, myOutput*, myRuntime*, const myConfig*);
myControl is a function that takes 4 parameters.
The function does not return a value.
Each of the parameters is a pointer to a type (presumably a structure). Some of the structures are const (unchangable). Some of the structures can be changed.
The full function prototype in the conventional form with the parameter names would look like:
void myControl(const myInput* inputData, myOutput* outputData, myRuntime* runTimeInfo, const myConfig* configuration);
parameter inputData is a pointer to a structure of type myInput.It cannot be changed.
parameter outputData is a pointer to a structure of type myOutput.It can be changed.
parameter runTimeInfo is a pointer to a structure of type myRuntime.It can be changed.
parameter configuration is a pointer to a structure of type myConfig.It cannot be changed.
The line
void myControl(const myInput*, myOutput*, myRuntime*, const myConfig*);
is a function declaration.
Until C23, one of the key syntactical differences between a function declaration and a function definition, is that in a function declaration the identifier (what you refer to as the "variable name") of each parameter in the parameter list is optional.
On the other hand, the identifier of each parameter is required in a function definition:
void myControl(const myInput* input, myOutput* ouput,
myRuntime* rt, const myConfig* config)
{
/* use the arguments, etc. */
(void) input;
(void) output;
(void) rt;
(void) config;
}
(C23 will introduce unnamed parameters in function definitions to the language.)
What you see in the example are simply the types that form the function's signature, where each parameter is a pointer type as indicated by the * symbol.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to write a lib wrapper. My wrapper function prototype looks like this
int my_wrapper(char * configstr);
The function to be wrapped looks like this.
void my_function(char config[CONFIGSTRSIZE]);
I tried to do the following:
int my_wrapper(char * configstr)
{
my_function(configstr);
return 0;
}
This compiles and works but I receive an error. Telling my that the argument is a invalid type.
discards qualifiers from pointer target type
Is there a way to perform a cast to telling the compiler that the pointer of the calling function always is a pointer of the proper size?
It's more or less impossible to change the interfaces. I thank you for your feedback.
Is there a way to perform a cast to telling the compiler that the
pointer of the calling function always is a pointer of the proper
size?
It is not possible in C. Pointers do not have information about the size. You need to pass it as the additional parameter or have this information in the dereferenced object (for example first is the size, or termination value like C strings)
First of all, the following function declaration doesn't make much sense, although legal:
void my_function(char config[CONFIGSTRSIZE]);
It creates the wrong impression that you can somehow pass an array (with a fixed size). You can't in C. What actually happens is that C adjusts any array type in a function declaration to a corresponding pointer type, so this declaration ends up to be just
void my_function(char *config);
You could write it like this to emphasize that the pointer passed should point to an array (I prefer not to, but that's a matter of style):
void my_function(char config[]);
Now for your problem, according to the error message you get, you're probably trying to pass a const-qualified pointer here. Depending on what my_function() is doing, there are two solutions:
my_function() only reads the array. Then you should change the function prototype like this:
void my_function(const char *config);
my_function() also modifies the array. Then you can't pass some const data there, but you might take a copy of your data. Assuming the data is a string, one way to do it would be the following:
// having some const char *config
char *cfgcopy = malloc(strlen(config)+1);
if (cfgcopy) { // allocation succeeded
strcpy(cfgcopy, config);
my_function(cfgcopy);
// do something with the modified cfgcopy
free(cfgcopy);
}
Instead of malloc()/strcpy(), many implementations provide a "shorthand" strdup() function you could use. But be aware strdup() isn't part of standard C.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I get the error for the two arguments of my function:
//incompatible type for argument of swapStruct
//expected Men100mParticipant but argument is of type struct <anonymous>
My code is like this:
int main(){
...
swapStruct(phaseToBeSorted->phase_result[j-1], phaseToBeSorted->phase_result[j]); //error
...
}
phaseToBeSorted is of type Men100mPhaseDetails that is defined as:
typedef struct Men100mPhaseDetails{
char* nameCurrentPhase;
int current_phase;
Men100mParticipant phase_result;
} Men100mPhaseDetails * Men100mPhaseDetails;
While pase_result is supposed to be an array of Men100mparticipant. The typedef is given as is and I can't change it.
This is the declaration of Men100mparticipant:
typedef struct {
char nameOfParticipant[MAX_LEN_NAME];
double* scores[4];
} Men100mparticipant, *Men100mParticipant;
and this the declaration of the function swapStruct:
static void swapStruct(Men100mParticipant a, Men100mParticipant b);
I don't understand what is the problem and I'll be glad to get some help in solving the problem.
You need to use:
swapStruct(&phaseToBeSorted->phase_result[j-1], &phaseToBeSorted->phase_result[j]);
The compiler is doing its best, but the structure type names are confusing.
It actually work but why is a '&' needed?
The type of phaseRoBeSorted->phase_result[j-1] is Men100mparticipant with two lower-case p's — so it is a structure. The swapStruct function takes two arguments of type Men100mParticipant (with an upper-case P), so it takes two pointers. The & passes the address of the array elements, of course, so that the function gets pointers, not copies of the structure.
This typedef (which appears to be given by the course instructors and can't be changed) is gruesome:
typedef struct { … } Men100mparticipant, *Men100mParticipant;
The case of the first letter P determines whether the type is a pointer or not, which is a disaster in the making. In general, Is it a good idea to typedef pointers? says "No". When there is a need (I'm not convinced this is an example of when it might be needed), make the difference clearer than the use of upper-case vs lower-case part way through the name.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Is it legal to name a function same as that of a user defined data type with enum ,in C?
For Example:
enum sentence_id sentence_id(const char *sentence, bool strict);
and enum sentence_id is defined as given below
enum sentence_id {
MINMEA_INVALID = -1,
MINMEA_UNKNOWN = 0,
MINMEA_SENTENCE_RMC,
MINMEA_SENTENCE_GGA,
MINMEA_SENTENCE_GSA,
};
Is it applicable to other user defined data type(as structure)?
This only looks strange, because the identifier for the enumeration and the function are identical, which is possible, because a enumeration identifier always has to follow an enum keyword and an enum always is followed by an identifier.
So let's give them different names:
enum minmea_sentence_id_e
{
…
};
This defines an enumeration with the name minmea_sentence_id_e.
The function gets another identifier, too:
enum minmea_sentence_id_e minmea_sentence_id_f(const char *sentence, bool strict);
Now it should be quite clear that there is a function called minmea_sentence_id_f returning a value of enumeration named minmea_sentence_id_e.
I do not understand the meaning of this statement
So I assume this is not your code. Let's see what the other person wrote:
enum minmea_sentence_id { ... };
In (Objective-)C this declares an enumeration type, minmea_sentence_id which is used by prefixing the name with enum. So for example:
enum minmea_sentence_id mySentenceID;
declares a variable of type enum minmea_sentence_id named mySentenceID.
Note that if instead of the above they had written:
typedef enum { ... } minmea_sentence_id;
then minmea_sentence_id would be a type name by itself and the above example declaration would just have been:
minmea_sentence_id mySentenceID;
However they didn't do that, so you need the enum when using the type.
why is it repeated twice?
They wrote the function declaration:
enum minmea_sentence_id minmea_sentence_id(const char *sentence, bool strict);
The first item in a function declaration is the return type, and from the above we know that is the first two tokens enum minmea_sentence_id.
Next comes the name of the function being declared, minmea_sentence_id, which looks strange – naming a function the same as a type – but is not illegal at all, though usually strongly discouraged as it makes code much harder to read!
So that's why it is repeated twice, but why might they have done this?
We can only guess, but (Objective-)C++ supports function-style casts, e.g.:
int x = (int)expo; // valid in (Objective-)C(++)
int y = int(expr); // valid in (Objective-)C++ and NOT in (Objective-)C
Now consider what a call to the minmea_sentence_id function looks like:
enum minmea_sentence_id mySentenceID = minmea_sentence_id("my sentence", TRUE);
a bit like a function-style cast. So maybe that is what they were thinking, but it's only a guess!
HTH
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I'm going through K&R and I'm on the functions chapter, and I have a quick question:
Do I always have to declare functions as prototypes? What decides what kind of arguments will be in the prototype? Can it just be two variables in the function definition?
Thanks!
You should always declare and define your functions using prototype syntax; doing so allows the compiler to catch errors where you pass the wrong number or types of arguments in the function call. C still supports declarations and definitions that don't use prototype syntax, but that's only to support ancient code bases; you should not write new code using the old syntax.
As for what arguments go into the prototype, that depends entirely on what the function needs to do. Suppose I'm writing a replacement for the pow() function in the math library. I need 2 arguments, one being the base and the next being the exponent:
double myPow( double base, int exp ); // declaration, prototype syntax
...
double myPow( double base, int exp ) // definition, prototype syntax
{
...
}
You can omit the parameter names in the declaration:
double myPow( double, int );
What matters is that the number and types of the arguments is specified.
The old-style declaration and definition would look like this:
double myPow( ); // declaration, old syntax
...
double myPow( base, exp ) // definition, old syntax
double base;
int exp;
{
...
}
The only time you don't choose the arguments for a function you're defining is the main function; while you get to define the implementation of main, you do not get to decide what arguments it will take. main either takes no arguments:
int main( void )
or two arguments of type int and char **:
int main( int argc, char **argv )
An implementation may provide additional prototypes for main (such as a third char **envp argument found on some Unix implementations), but that's limited to the implementation - you are not allowed to create an arbitrary interface for main.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to pass a single pointer and double pointer in a function. but its giving me error.
int main()
{
int *pt;
fun(pt);
.
.
}
fun(int *pt)
{
pt=(int*)malloc(6*sizeof(int));
.
.
}
and what is the syntax when we are using double pointer. Can anybody just describe it with an example or can edit the above example. I shall be very thankful to you.
The fundamental idea of reference semantics is that a function modifies some other object that exists outside the function's own scope. You can implement reference semantics in C by passing the address of the object that is being referenced to a function that takes an argument of type "pointer to the type of the object".
The crucial hallmark of "reference semantics via pointers" consists of these two points:
The caller takes the address of something (via the &-operator).
The callee dereferences the argument.
For example:
Caller:
T x;
f(&x); // address-of
Callee:
void f(T * p) // take argument as pointer
{
(*p).y = 20; // dereference (via *)
p->x = 10; // dereference (via ->)
}
In your situation, T = int *:
int * pt;
fun(&pt); // function call takes address
void fun(int ** p) // function takes pointer-to-object...
{
*p = malloc(173); // ...and dereferences to access pointee
}