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
}
Related
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.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I want to call a function that calculates the width of a circle.
#include <stdio.h>
void area(double*);
int main()
{
void* vp = NULL;
double r;
printf("input value : ");
scanf(" %lf", &r);
(double*)vp = &r;
void area(vp);
}
void area(double* dp)
{
double result;
result = (*dp) * (*dp) * (3.14);
printf("circle are is : %.2lf", result);
return 1;
}*
I want to call a function in the //void area (vp)//, but I can not catch the error in visual stdio and proceed as it is. Do you know what the cause is?
To call the function, you write area(vp); not void area(vp);.
The latter, as it appears in the function main, is a function prototype and has no run-time effect.
And fix that definition of PI: yours is terribly inadequate given that you use a double type. See Math constant PI value in C
You should write
area(&r);
instead of the wrong and superfluous
void area(vp);
Adding to the obvious mistake you made in the syntax
I don't see any reason for vp to be there.
to ensure success for scanf(), always check for the return value of the call.
Quoting C11, chapter ยง6.8.6.4,
A return statement with an expression shall not appear in a function whose return type
is void. [....]
There are three problems with the code.
For starters it is unclear why the pointer vp is declared as having the type void * though in the program it is used only to point objects of the type double.
Why not to declare it like
double *vp = NULL;
As result this trick with the pointer
(double*)vp = &r;
will not compile because in the left side the expression (double*)vp is rvalue and may not be assigned.
You could just write
vp = &r;
The second problem is that this statement
void area(vp);
is also invalid. To call the function you should write
area(vp);
And the third problem is that a function that has the return type void shall not specify an expression in the return statement
This statement shall be removed.
return 1;
From the C Standard (6.8.6.4 The return statement)
1 A return statement with an expression shall not appear in a
function whose return type is void. A return statement without an
expression shall only appear in a function whose return type is void.
There is no sense to declare the parameter of the function as having a referenced type.
The function definition could look like
double area( double dp )
{
const double PI = 3.14;
return dp * dp * PI;
}
The statement with the call pf printf should be moved in main
printf( "circle are is : %.2lf", area( r ) );
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I want to know the benefits of function pointers in computer and embedded systems programming.
Same as regular typdefs, incredibly useful for 2 and 4. Instead of typing the whole thing, you can now use myFuncDef
A function declaration for a function that recieves a function pointer. A function that takes a function pointer of 1. would thus look int add2to3(myFuncDef functionPtr);
-
A function that takes an int and (probably based on that) returns you a function pointer. Using the typedef it would have looked like myFuncDef functionFactory(int n);
Without typedefs these chains of pointers to pointers could get incredibly long as can be seen in this question: C syntax for functions returning function pointers
1) typdef of function pointer will help to return a function pointer from a function.
i.e., typedef int (*myFuncDef)(int, int);
myDuncDef retFunc() here retFunc return the function pointer.
2)int add2to3(int (*functionPtr)(int, int)); here add2to3function gets the function pointer with specified type as a argument.
3) ------
4) int (*functionFactory(int n))(int, int) this is another way of returning a function pointer.
typedef(s) are used to simplify declarations.
This declaration
int add2to3(int (*functionPtr)(int, int));
declares a function that accepts as argument a pointer to a function with two parameters of the type int and has the return type int.
To make this declaration simpler you can write the parameter as having a type of function instead of the type pointer to function.
int add2to3( int function( int, int ) );
Or you can use the typedef
typedef int (*myFuncDef)(int, int);
//...
int add2to3( myFuncDef functionPtr );
This declaration
int (*functionFactory(int n))(int, int);
declares a function that has one parameter of the type int and has the return type pointer to a function that has two parameters of the type int and has the return type int.
Again using the typedef above you can simplify the declaration
typedef int (*myFuncDef)(int, int);
//...
myFuncDef functionFactory(int n);
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
Is the following code in C correct? I am not 100% sure about the scope of function parameter "a":
void method(int a)
{
MyStruct mystruct;
mystruct.value = (void*) (&a);
nested_method_call(mystruct);
}
Let's assume it's:
void method(int a)
{
MyStruct x;
struct.value = (void*) a;
nested_method_call(x);
}
since struct cannot be used as a variable name.
a's scope is the function, so it is alive during nested_method_call, however, that is absolutely irrelevant because your nested_method_call isn't referring to it -- it's using a copy of a reinterpreted as void*. (Such reintepretation makes the code quite unportable and unusual, by the way. It might be a mistake.)
Edit:
void method(int a)
{
MyStruct mystruct;
struct.value = (void*) &a;
nested_method_call(mystruct);
}
is valid (though it's usually a good idea to pass structs by pointer because it's faster for any but very small structs). &a is a valid reference until the } of method.
You also shouldn't need that cast. If .value is void*, you don't need it. If it isn't, the cast makes &a convertible to any other data pointer, which is potentially a dangerous hole in the type system.
First of all, struct is a keyword in C and cannot be used as a variable name, so this is wrong code.
After that, casting an integer to pointer (and vice-versa) is implementation defined behaviour. You should better use uintptr_t for better, if you have to get that conversion done.
That said, from the basic premises of your question, the logic behind the code seems legit, there's no problem with the scope. In case you meant to ask about the lifetime ["The lifetime of an object is the portion of program execution during which storage is
guaranteed to be reserved for it"], then, it is until the end of the method() function and you're using it inside that limit, so you're good to go.
Corrected code: struct.value = (void*)&a;. Also some other validation needs to be done.
So, if you fully correct your code so that it complies properly then Correct word would be Lifetime instead of Scope. Lifetime of a will be till the end of function method. Therefore, a can still be accessed inside nested_method_call via pointer struct.value assuming type of struct.value is int*.
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
New to understanding structs, especially involved with accessing a struct member's address or value.
I'm not sure about my use of language here, but I have:
a 2x2 matrix of struct a
struct x is an array of size 2
struct b of size 2 holds the answers
So far I have...
&(*(b+i))->s = (*(*(a+i)+j)).s * (*(x+k)).s
however I receive the compiler error
error: lvalue required as left operand of assignment
EDIT:
My struct
typedef struct num{
int s;
}num_t;
My function parameters
void calc(int n, int m, num_t **a, num_t *x, num_t *b)
Also, is the question phrased correctly?
EDIT2: Format
Assuming I understand your question correctly, the issue has to do with operator precedence. As written, the left operand of the assignment is equivalent to:
&((*(b + i))->s)
In other words, you're trying to assign a value to the address of a variable, because the -> (pointer to member) operator has a higher precedence than & (address of) operator. What you want instead is
(&(*(b + i)))->s
However, this is grossly unnecessary, as the -> operator expands to the * dereference operator and . member operator, so you get
(*(&(*(b + i)))).s
The outer * dereference and & reference operators, in this instance, effectively "cancel" each other out. So, you can rewrite it as just
(*(b + i)).s
Finally (and this rule can be applied to the other two pointer expressions), this is basically accessing an array element, so it's much clearer to write this as
b[i].s
So, your final statement should look like this
b[i].s = a[i][j].s * x[k].s;
This is much cleaner, and much more effectively conveys the intent of the statement, which is always important in programming.