Can you explain the code below? How we can use #define for a keyword of C?
#include <stdio.h>
#define int int*
int main(void) {
int *p;
int q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}
Output:
10 5
This #define int int* is a preprocessor macro. If you want to define your own synonyms for types then use typedef. You cannot create keywords in a language you did not create.
Sample:
#include <stdio.h>
typedef int * myIntPtr;
int main(void) {
int i = 10;
myIntPtr x = &i;
printf("%d", *x);
return 0;
}
Output:
10
Also semantically making an int to an int * makes no sense.
#define a b
will change all "a" to "b" in your source after the #define
#include <stdio.h>
#define int int*
int main(void) {
int *p;
int q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}
will change to
int main(void) {
int **p;
int *q;
p = 10;
q = 5;
printf("%d %d", p, q);
// your code goes here
return 0;
}
Related
The explanation below confused me:
When an argument is pointer to a variable x, we normally assume that x will be modified :
f(&x);
It is possible, though, that f merely needs to examine the value of x, not change it.
I tired to understand and the code below can't work.
#include <stdio.h>
void function(int& a)
{
a = 5;
}
void func(int b)
{
b = 5;
}
int main(void)
{
int x = 0;
function(x);
printf("%d", function(x));
func(x);
printf("%d", func(x));
return 0;
}
Code refer from the second answer:
int f(int &a){
a = 5;
}
int x = 0;
f(x);
//now x equals 5
int f2(int b){
b = 5;
}
int y = 0;
f2(y);
//y still equals 0
An example actually using f(&x):
#include <stdio.h>
void f(int *p) {
*p = 4;
}
int main(void) {
int x;
f(&x); // Provide a pointer to `x`.
printf("%d\n", x); // 4
return 0;
}
Both of your program use int &a, which isn't a valid C declaration. That is why they don't even compile.
The first codeļ¼
#include <stdio.h>
int *func() {
int n = 100;
return &n;
}
int main() {
int *p = func(), n;
n = *p;
printf("value=%d\n", n);
return 0;
}
output:
value=100
The second code:
#include <stdio.h>
int *func() {
int n = 100;
return &n;
}
int main() {
int *p = func(), n;
printf("c is an interesting language!\n");
n = *p;
printf("value=%d\n", n);
return 0;
}
output:
c is an interesting language!
value=30
I use the debugging program and found that the printf function changed the value of n. What operation does it do inside? If anyone could give me a detailed answer, I would be grateful!
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct a{
int length;
};
static int a2(int a[]){
int y = 0;
int x = 0;
for (int i=0; i<a.length; i++)
{
if (a[i]%2 == 0)
y += a[i];
else
x += a[i];
}
return x - y;
}
int main()
{
int a[] = {1};
printf("%d\n", a2(a));
return 0;
}
when I run this code I receive the following error "error: request for member 'length' in something, not a structure or union" can anyone help me to understand the error and how to rectify the code? Thanks
The name of structure and the name of variables are not related.
The argument a is a pointer (int a[] in function arguments has the same meaning as int* a) and it doesn't have members.
You have to pass the length of array to pass to functions aside from (the pointer to the first element of) the array.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int a2(int a[], int a_length){
int y = 0;
int x = 0;
for (int i=0; i<a_length; i++)
{
if (a[i]%2 == 0)
y += a[i];
else
x += a[i];
}
return x - y;
}
int main()
{
int a[] = {1};
printf("%d\n", a2(a, sizeof(a) / sizeof(*a)));
return 0;
}
I have the following code:
#include <stdio.h>
#include <stdlib.h>
int f(int x, int *py, int **ppz) {
int y, z;
**ppz += 1;
z = **ppz;
*py += 2;
y = *py;
x += 3;
return x + y + z;
}
int main(void) {
int c = 4;
printf("f(): %d\n", f(c, &c, &&c));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
How can I access **ppz correctly, because so I get an error message: "label 'c' used, but not defined".
An int** is a pointer to an int*. You need to create a variable of type int* to be able to pass a pointer to it somewhere. Here is what you should do:
int main(void) {
int c = 4;
int* pc = &c;
printf("f(): %d\n", f(c, pc, &pc));
printf("c: %d\n", c);
return EXIT_SUCCESS;
}
Refer #ikegami's answer for an explanation of the proper use of a pointer to a pointer.
You want to modify a variable of type int, so the parameter should be int *, not int **.
You'd use int ** if you wanted to modify a variable of type int * variable. That's not the case here.
For example,
void f(int **pp) {
*pp = malloc(10);
}
int main(void) {
int *p;
f(&p);
// ...
free(p);
}
I have a funtion_ptr function pointer which point to add_int function.
case 1: when lay a statement function_ptr = &add_int outside main function
--> compiler error: error C2373: 'function_ptr' : redefinition; different type modifiers (this is in )
#include <stdio.h>
int add_int(int n, int m){
return n + m;
}
int(*function_ptr)(int, int);
function_ptr = &add_int; // it's here
void main(){
int sum = (* function_ptr)(2, 3);
printf("sum = %d", sum);
_getch();
}
case 2: function_ptr = &add_int; in main function --> it is true
#include <stdio.h>
int add_int(int n, int m){
return n + m;
}
int(*function_ptr)(int, int);
void main(){
function_ptr = &add_int; // it's now here
int sum = (* function_ptr)(2, 3);
printf("sum = %d", sum);
_getch();
}
Could anyone explain for me different between the two case.
Thanks!
function_ptr = &add_int; is an assignment statement. Statements are allowed inside functions, but outside functions only declarations are allowed. Since assignment is not a declaration, the compiler issues an error.
If you want to assign the pointer as part of its declaration/definition, you could combine the declaration and the assignment, like this:
#include <stdio.h>
int add_int(int n, int m){
return n + m;
}
int(*function_ptr)(int, int) = &add_int;
int main(){
int sum = (* function_ptr)(2, 3);
printf("sum = %d", sum);
return 0;
}
Demo.
You don't need the & in the assignment statement, the function's name (add_int) IS its address... and the assignment statement should be within the bounds of a function.
Here's an example that works:
#include <stdio.h>
int add_int(int n, int m);
int(*function_ptr)(int, int);
void main(){
function_ptr = add_int;
int sum = (* function_ptr)(2, 3);
printf("sum = %d", sum);
getc(stdin);
}
int add_int(int n, int m){
return n + m;
}