I tried like
int a=b=3;
but the compiler says 'b' undeclared.
Please help, thanks.
You can use , to declare the variables in a single line first
int a, b, c, ... , last;
and then you can assign to them all at once
a = b = c = ... = last = 3;
note that
int a = b = 3;
which is equivalent to
int a = (b = 3);
will declare a and initialize it with the result b = 3, which in turn will assign 3 to b, which is UNDECLARED
No you can't do this because the compiler interprets that the value 3 to bshould be assigned and b is undeclared
You can do
int b;
int a=b=3;
Related
Why my code output is 5 and 0, not 6 and 5?
I think I should get 6 and 5. I am a beginner.
#include <stdio.h>
int swap(int a,int b);
int main()
{ int x =5;
int y =6;
printf("%d %d\n",x,y);
int number[2]={swap(x,y)};
x=number[0];
y=number[1];
printf("%d %d\n",x,y);
return 0;
}
int swap(int a,int b)
{
return b,a;
}
There are several things wrong here.
First, you can't return multiple values in C, like you can in Python. return b, a; uses the comma operator, which evaluates both its operands and returns the second one. So this is equivalent to just return a;.
Second, your array initializer is only initializing the first element of the array. There's only one expression in the initialization braces, so that initializes number[0]. The remaining elements of the array are initialized by default to 0.
Combining both of these, it's equivalent to:
int number[2] = {y, 0};
I can see you are new to C programming. The problem is in your swap() function. You're using a language construct that does not exist in C, namely tuples. Check out pointers for a proper way to return multiple values from a function.
This function ...
int swap(int a,int b)
... returns one int, as its prototype says.
This statement ...
return b,a;
... involves C's comma operator ,, which evaluates its left-hand operand, discards the result, then evaluates to the value of its right-hand operand. Since evaluating b has no side effects in your case, that return statement is equivalent to
return a;
In C, it is valid to initialize an array with fewer explicit elements than the length of the array. For an automatic (local, non-static) array such as yours, as long as at least one element is initializer, all elements not explicitly initialized are implicitly initialized (to 0 in the case of int elements). Thus, for your implementation of swap(), this ...
int number[2]={swap(x,y)};
... is equivalent to
int number[2] = { x, 0 };
, which explains the output.
Here is a way to solve your problem.
#include <stdio.h>
void swap(int *a,int *b);
int main()
{
int x = 5;
int y = 6;
swap(&x, &y);
printf("post swap x = %d, y = %d\n", x, y);
return 0;
}
// No need to return anything, we change the x, y values using the pointer
// This is passing by reference. Instead of passing the value, we are
// passing the reference (i.e address of the variable). swap function can
// now directly access the values and change them
void swap(int *a, int *b)
{
int tmp;
printf("Swap got a = %d, b = %d\n", *a, *b); // Note: we access value of a pointer using * in front of the pointer varaible
tmp = *a;
*a = *b;
*b = tmp;
}
outputs:
bhakta: /tmp$ cc x.c
bhakta: /tmp$ ./a.out
Swap got a = 5, b = 6
post swap x = 6, y = 5
I want to learn very basics of pointers in C language
What is the difference between below two ways? Which is correct? Which is more preferable?
int a = 20;
int *p = &a ;
or
int a = 20;
int *p ;
p = &a ;
The difference here is not related to pointers, but to declaring and initializing variables in general.
For example, you can do:
int a; // this declares the variable a as an integer
a = 20; // this initializes the variable a with the value 20.
OR, you can combine these two into one line:
int a = 20; //this now both declares and initializes the variable a.
The difference is that you can only declare a variable ONCE, but you can assign a value to it as many times as you like.
So if you were to write
int a = 20;
and then later on in your code you wanted to change the value of a to say, 30, here you can ONLY write
a = 30;
You could not write int a = 30; again, because you cannot declare a again, a has already been declared.
This difference is what you are illustrating with your pointers.
int a = 20; //variable a is declared as an int and also initialized to the value 20
int *p = &a ; //pointer p is declared and initialized with the address of a.
or
int a = 20; // variable a is declared as an int and also initialized to the value of 20
int *p ; // pointer p is declared
p = &a ; // pointer p is assigned the value that is the address of variable a.
You could also have written
int a;
a = 20;
int *p;
p = &a;
And this is still correct, and produces exactly the same result.
our teacher gave us the following code:
x = **(&d);
His question was: "Which lines of codes do you have to add above this line, so that the code is correct?"
Can anybody help me? What is the meaning of this line?
The remark of DeiDei is a possibility, I just explain more here
We are in C, and to simplify consider x and d are not macros.
x = **(&d); is equivalent to x = *d; because to get the address then dereference does nothing
Now the question is to find a context where x = *d; is legal, for that d has to be a pointer. Let say int * d;
Probably also the goal is to not have a segmentation fault so d need to memorize a valid address. Let say int a; int * d = &a;
Now we assign x with *d so the type of x must be compatible with int Let say int a; int * d = &a; x = **(&d);
To be clean we do not want to access to an uninitialized value, because x finally receive the value of a that one must be initialized.
int a = 0; int* d = &a; int x; x = **(&d); from DeiDei is compatible with the requirement, but of course they are plenty of other solutions
x = **(&d);
is equivalent to
x = **&d;
is equivalent to
x = *(*&d);
also for any variable v this
v == *&v
holds true.
From the above it concludes that
*&d == d
so
x = *(*&d);
equals
x = *d;
As the dereference operator * can only be applied to a pointer it follows the d has to be of a pointer type
T * d; /* Let be T any type. This defined d to be pointer to that type T. */
Applying to a variable of type T* the operator * the result evaluates to the type T itself.
As it is given
x = *d
from this it concludes that x needs to be of type T.
So the line in question needs to be
T x, *d;
or in a more readable form
T x;
T * d;
:-)
I m learning C programming and I have seen some code where folks use double pointers. I did some searching on stackoverflow from below but still have a teeny tiny question (Why use double pointer? or Why use pointers to pointers?)
In the code below, my question is I have not defined 'b' as **b. Now
when I try to do (*b) I`m getting a compile error. Isnt this *(*b) same as
*(address of c) because 'a' has address of 'c'.. Why do I need to define 'b' as **b to really get the value of 'c'?
#include <stdio.h>
int main()
{
int *a, *b, c;
c = 10;
a = &c;
b = &a;
printf("*a - %d\n *(*b): %d\n," , *a, *(*b));
return 0;
}
Help/explanation is really appreciated :)
EDIT : Thanks a lot guys for the explanation. Things are now clear :)
Here is a working code.
int main()
{
int *a, *b, c;
c=10;
a = &c;
b = a;
printf("*a - %d\n *(*b): %d\n,",*a, *(b));
return 0;
}
Pointers are aptly name: they "point" to locations in memory.
When you write *b, you tell the compiler that you are going to declaring a pointer of type integer so that it points to an integer.
When you use b = a, you tell the compiler that you are assigning the address of c to a as well. Actually, b is a pointer variable itself which is storing the address of an integer variable c. So, this way, you can assign the address of one variable to another pointer as well.
Now, Regarding your code,
int main()
{
int *a, **b, c; // just change it from *b to **b
c = 10;
a = &c;
b = &a;
printf("*a - %d\n *(*b): %d\n,",*a, *(*b));
return 0;
}
You need to specify the compiler that the pointer b is a pointer to a pointer variable by writing 2 *.When you write **b, you are telling the compiler that you are pointing to another pointer variable. Similarly, you can also have triple pointers and Quadrupled pointers as well.
Here is a link for your reference. http://www.c4learn.com/c-programming/c-double-pointer/
In your code, you have to do int **b; because:
a is a pointer-to-integer, since a = &c.
b is a pointer-to-(pointer-to-integer), since you wrote b = &a.
You can access the value of c using *b.. change your code littlebit..
#include <stdio.h>
int main()
{
int *a, *b, c;
c = 10;
a = &c;
b = a;
printf("*a :%d\n *b: %d\n,",*a, *b);
return 0;
}
*b,*a
defines that a and b are two pointer variable..
a=&c;
Assign the address of the variable c in pointer a.. & stand for address of operator.
b=a;
Copy the contains of a in b.. Now b also contain the address of c..
Print *a and *b.. *a and *b means contain of a and b. So *a and *b will print value of c..
You can also use **b.. **b means b is a pointer which stores address of another pointer variable..
In that case the code will be,
#include <stdio.h>
int main()
{
int *a, *b, c;
c = 10;
a = &c;
b = &a;
printf("*a :%d\n *b: %d\n,",*a, *(*b));
return 0;
}
a contains address of c and b contains address of a.. So, *a will print value of c..
As b is a double pointer we need *(*b) to access the contain of b.. *(*b) will also print the value of c..
Came across this example in the book im reading and it didn't make sense at all to me, I'm probably missing something but it seems like youre assigning count with the values '10' and then the value 'x' which isnt even an int. Just wondering if this is a syntax that is valid.
The book says this:
The variables count and x are declared to be integer variables in the normal fashion. On the next line, the variable intPtr is declared to be of type “pointer to int.” Note that the two lines of declarations could have been combined into a single line:
int count = 10, x, *intPtr;
here's the program its taken from:
#import <Foundation/Foundation.h>
int main (int argc, char *argv[ ])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int count = 10, x;
int *intPtr;
intPtr = &count;
x = *intPtr;
NSLog (#"count = %i, x = %i", count, x);
[pool drain];
return 0;
}
This is just a declaration. Declaration consists of the initial part (declaration specifier) that describes the "basic" part of the type, and a comma-separated sequence of declarators, each of which declares a separate name and, possibly, modifies the basic type. In C you can declare multiple names using the same declaration specifier
int count, x, *intptr;
is equivalent to
int count;
int x;
int *intptr;
Optionally, you can add an initializer to each declarator or to some fo them. So
int count = 10, x, *intptr;
is the same as
int count = 10;
int x;
int *intptr;
That's all there's to it.
It's equivilent to
int count = 10;
int x;
int *intPtr;
This is a common source of errors for both novice and expert C (and C++) programmers, together with comma operator.
Usual confusion:
int* p, pp;
pp = malloc( sizeof( int ) * N ); /* oops pp is just int, not a pointer */
...
int x;
x = 12, 13, 14; /* and the value of the x is ... */
Comma means, that x is also an int, and *intPtr is also int (and intPtr is pointer to int).
Its like:
Aunts Mary, Jane, Daryl;
Mary is stupid;
Jane is Old;
Daryl is same as Mary;
Its like
int count, x;
except that count is being initilized to 10