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 8 years ago.
Improve this question
I remember when I first started learning C somebody told me that "pointer arithmetic is hard." I don't exactly understand what is hard about it though?
arr[i] == *(arr + i)
*ptr = arr[2] then *ptr + 1 = arr[3]
Is this really all there is to it, because neither of these concepts is as hard as I expected?
Well, if you mean *(ptr + 1) = arr[3] for that last one, then it seems like you have the hang of it. Making the substitution of the subscript operator ([]) for a pointer:
arr[3] === *(arr + 3)
Isn't very difficult. What gets tricky is when you have pointers point to the middle of an array, and then have to remember where they are in the array so you know what to index. So if ptr1 points to the beginning of an array, and ptr2 points at index 3, then *(ptr1 + 4) and *(ptr2 + 1) each yield the same value. In addition, if you have pointers in the middle of an array, you can subscript with negative numbers, something that isn't allowed in higher level languages like Java. So yes, it can get as complicated as one cares to make it.
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 3 years ago.
Improve this question
I don't get what I should put in the place of: "α" and "β" in order to print all the 3 digit numbers which are different from 0 and from one another.
It is required to replace those 2 variables, with some code
We are entering G(0)!!!
It's from an exam paper, I really don't get it, please help.
void G(int k)
{int i;
for(i=1;i<=α;i++)
{ p[k]=i;
if(β)G(k+1);
else
printf("%d%d%d\n",p[0],p[1],p[2]);
}
}
For any of this to make sense, it must be that p is declared as a global int array of dimension at least 3. I assume for the purposes of this answer that it is in fact so declared.
Note that the function sets p[k] = i, but it later reads back only p[0], p[1], and p[2]. This should give you a pretty good idea about what makes sense for expression β, which controls whether to recurse (increasing k) or print.
Note also that the function sets p[k] = i, and that when it reads back those p[k] for various k, it wants to get values ranging from 1 to 9 (no more and no less). This should give you a pretty good idea of what expression makes sense for α, the inclusive upper bound on i.
Having figured those out, it remains to satisfy yourself that the natural substitutions for those expressions indeed produce a resulting function that behaves as required when initially called as G(0). I suspect that you will find that easier than you did discerning the needed expressions.
(Details are left as an exercise.)
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
The above expression will breakdown like *(&(&(&(*(*(*X)))))
As far as I could solve I think the compiler can trace the value at (***X). But what happens when it tries to retrieve the address of the value i.e'; &(***X). Will this work? Because the value is some random number is an expression. Will the compiler search for the value and return its address? If not what will be the output?
A compiler error. You can't dereference an int.
Your assumption is incorrect: *&&&***x will not break down like *(&(&(&(*(*(*X))))), it will be tokenized as * && & * * * x, resulting in a syntax error.
Adding spaces as in *& & &***x will result in a constraint violation because you cannot take the address of an address, nor can you dereference the int variable x as a pointer.
Conversely, *&*&*&*&x is a correct albeit contorted way to take the value of x, and so is 0[&x].
no, the problem occurs since x is of type int. it contains the value 2. when you say *x, it means "I want the value of the variable that x points to." and you know 2 is not a valid address in memory. this cause error.(the only valid address that you can assign is 0.)
also, since x is of type int, you can't dereference int.you will got a compile error.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
Can anyone explain what will be the output of following code and why?
int main()
{
int i[] = {3, 5};
int* p = i;
int j = --*p++;
printf("%d\n%d\n", j,*p);
system("pause");
return 0;
}
I'm actually going to answer this question, even though it's true that "the OP simply needs to compile the code and run it", because it isn't obvious to a beginner that this is one of the cases where that gives the right answer. People get dinged all the time on this site for asking why superficially similar code (that does have undefined behavior) gives a surprising answer.
int j = --*p++;
means
int j = *p - 1;
*p = j;
p += 1;
That is, *p (the value that p points to) is decremented, the decremented value is written to j, and then p itself is incremented.
There is no undefined behavior here because the -- and ++ operators in the original form are acting on two different data objects. The expressions that have undefined behavior due to multiple use of the side-effect operators all involve modifying the same data object twice in the same expression ("without an intervening sequence point").
(If you're wondering why the -- applies to *p but the ++ applies only to p, the answer is "because that's what the grammar of the language says. Sorry, this is just one of those things you have to memorize.")
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
What's really happening behind?
int a=10,b=5,c=3;
b!=!a;
c=!!a;
Why the values of b and c are 5 and 1 respectively?
b is 5 because you've assigned it that value and never changed it, since
b!=!a;
...is just a condition that you don't do anything with, not any form of assignment.
c is 1 because a is 10 and !10 is 0, and !0 is 1, thus !!a is 1 (a is 10).
You already have got the answer to your question, but just to have a deeper look into why it happens that way, let me add my two cents here.
An expression like
b!=!a;
is same as
b != !a;
because of the maximal munch rule used in the translation phase. Basically, this says that the longest possible token should be selected from the input while creating a construct (obviously, valid/ meaningful).
Following this principle the ! and = are considered together to form the perfectly valid operator != and the expression is parsed like
b != !a;
over
b ! = !a; //or anything else.
That is why, there is no assignment, as you might have thought.
That said, ! is an unary operator, which does not change the value of the operand. So, taken together, your code is essentially same as
int a=10,b=5,c=3;
c=!!a; //double negation
so, a and b are unchanged, and c is 1 (because !!10 == !0 == 1 )
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 9 years ago.
Improve this question
Both a[i] and *(a + i) access the i th element of array a.
Are there reasons to prefer one over the other(performance, readability, etc...)?
Neither one is better from a language point of view since they are the same, array indexing should just be syntactic sugar for pointer arithmetic. We can see this from the draft C99 standard section 6.5.2.1 Array subscripting which says (emphasis mine):
[...]The definition of the subscript operator [] is that E1[E2] is identical to (*((E1)+(E2))).[..]
Although for others reading your code a[i] is probably more readable.
a[i] should be preferred, because it is more commonly used and so will be understood more quickly by the person reading the code.
Although they are identical in arrays, I prefer a[i] for the following reason. Suppose you have code like this:
double gradient[3];
// 200 lines later
double dx = gradient[0]; // Works fine
double dy = *(gradient + 1); // Works just as well
Then somebody decided that std::vector looks better than double[]:
vector<double> gradient;
// 200 lines later
double dx = gradient[0]; // Still works
double dy = *(gradient + 1); // Ouch!
Then somebody else decided to go object-oriented all the way:
class Gradient
{
public:
double operator[](int index) const;
};
// 1000 lines later
Gradient gradient;
// 200 lines later
double dx = gradient[0]; // Still works
double dy = *(gradient + 1); // Ouch!!!
Thus, from the viewpoint of maintainability, I prefer to say exactly what I mean, without obscuring the intent with context-dependent synonyms. If I mean "take i-th element of a" then I say exactly that in C++ lingo:
a[i];
rather than relying that for the particular implementation of a one can use a fancier expression for the same.
C has no operator overloading. So you are safe to use whichever you want.
I would use a[i] for clarity when reading the code.
Both are equivalent. Neither one is preferred over other but a[i] is commonly used by programmers. a[i] = *(a + i) = i[a]
As far as I'm concerned (just finished a C course), there is no difference.
Array is implemented as a pointer to the first element on the stack. So a+1 is just the address on the stack after a :)
Edit:
Also, as mentioned by John Bartholomew, even though they are the same, you might want to use a[i] instead - that is the "normal" way to do it (also in other languages) :)
For readability purposes, I would definitely go with a[i].