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 9 years ago.
Improve this question
is printf("%d",*(++(*a))); Undefined? Here a is pointer to a pointer to integer.
I do not have anymore code other than this.It is an extension of This question which had created lots of confusions.Just want to know what is happening in this print.Does it depend on Architechture(32 Vs 64) or compiler versions.
Hoping answers will be descriptive and clear.
If you break it down, it does this:
Take the value of what a is pointing at: *a
Increment by one ++(*a)
Dereference that *(++(*a))
So, if the value+1 of what is stored at a is a valid pointer, this will work. Otherwise, the result is undefined and will most likely result in a runtime error.
Yes, your code is correct and even if cryptic can make some (little) sense as in:
void print_next(int **a) {
printf("%d\n",*(++(*a)));
}
int arr[] = {1,5,6,3,5,6};
int *p = arr;
while (p<arr+6)
print_next(&p);
If your question is specifically about *(++(*a)) expression, then there's nothing undefined here (assuming all pointers involved are valid). There are no attempts to perform multiple modifications of the same object. There are no independent reads of any of the modified objects. End of story.
Basically, there's nothing to explain here, since the code is perfectly fine in a rather straightforward manner. There's really no room for anything more "descriptive and clear" than that.
If this is not sufficiently clear, you have to explain what exactly looks suspicious to you in this expression.
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 7 years ago.
Improve this question
I find that I refer to the value at the memory location pointed to by a pointer far more often then I want to refer to the actual value of the pointer. As a result I wonder why C does not use the asterisk in the to refer to the actual value of the pointer since it is more typing.
I have already read a post on another website about how B did it this way. This does not answer my question; it just changes it to why did B do it this way.
I doubt anyone can tell you why it was designed the way it was. But a pointer is a variable that holds an address. Therefore, it's value is the address. I would find it strange and unexpected if it required special syntax to get the value of any variable.
Note that C++ lets you use references to access the value pointed to without special syntax. But you are dereferencing the pointer, so for me it makes perfect sense to require a different syntax.
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 8 years ago.
Improve this question
I was giving interview in a company and they asked me "What are applications of null pointers in C"? I told them what null pointer is and how it leads to segmentation fault. However, they were not happy with the answer. I have never heard anything like this before. If anyone has any idea, please share.
Thank you
they were asking when you need to use NULL pointers, not when they lead to problems.
The classic answer is that they are used when there is nothing to point to. For example the next pointer in a list when its the end of a list -> there is no next item.
and so in your code you go
if(nextItem != NULL)
{
// do stuff with nextItem
}
else
{
// the end
}
As mooseboy points out this is a sentinel value i.e, a special value that your code recognizes as having a deciding value.
NULL is the perfect pointer sentinel since NULL is assumed to never be a valid value for a pointer (for example you could not use 0x000042 since 42 might be a real address)
This question is kind of vague, but one thing they might have been unhappy about is that from a pure language perspective, dereferencing NULL is not guranteed to cause a segfault. It causes unspecified behavior, i.e. the language doesn't say what will happen. On most platforms, yes, null reference causes a segfault, but an implementation of C could do something else and still conform to the standard.
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 have a pointer of pointers, and I want a loop to go thorough them and store its value into something else. Is there any way to do that?
e.g:
char **variable;
Now I want to read that into another variable:
char **variable2
i thought of doing something like this:
for(i = 0;i <LENGTH_OF_VARIABLE-1;i++){
variable2[i] = variable[i+1]
}
But that is not possible in c, right?
Now you might ask why not variable2 = variable? well variable2 should store only parts of the variable, not all of them.
EDIT: Variable's size is not known, and its dynamic(read from the command line). AND no it doesn't contain '\0' at the end. Cause its processed to remove such a character and then passed to a function that I am implementing.
If you already putting anything to your **variable, does that mean that you have allocated memory correctly?
I think it will better for you to revise and understand how simple one dimensional array works, after understanding that, move to double arrays. Then take a look how pointers work and learn how to allocate memory. After understanding this steps i have mentioned above, take a look at double pointers and allocation of memory in case of double pointers.
here you go.
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 have a problem with this code I am using in embedded C. It works perfect if I pass p as a parameter of some function but not works in case of "p" is local. Please help to understand in details how this code works.
The pointer p is cast to char** and dereferenced twice, ch is copied to that location, and *p is then incremented (points to the next character.
I imagine (because without code that is all I can do) that not working "in case of p is local" has little to do with p being local and everything to do with the value of p and the contextual semantics of the code in question. Whether the code works or not depends solely on p holding a valid value such that the double de-reference resolves to the intended location.
To be honest however, you should avoid writing such code, it's too "clever", and as Brian Kernighan said:
Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?
P is pointer to char pointer.Initially it is typecast to char** and dereferencing two times.After dereferencing 2times,at this place ch is copied.After that it is incremented by1.Here, to understand about incrementing such a pointer, try to understand follows.*p++is equal to *p and p++,NOT an equal to *p and (*p)++. Like this, your pointer is also incremented. There is chance to get segmentation fault in your copy statement.
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 9 years ago.
Improve this question
Basically I am a Java programmer, and not very well know about pointers in C.
so,
#include<stdio.h>
int main( ){
char*______Time______ = "world";
printf("%s",_____Time_____);
return 0;
}
I guess the output here should be: world ?
Is something spooky here which I should know?
Thanks for any help.
This should print "world", yes.
It looks a bit like it's trying to play with the GCC built-in preprocessor symbol __TIME__, but of course it's spelled wrong to do that.
I expect to see world, but your shell might then see that the last command ended without a newline and it might add something to signify that before starting its prompt on a fresh line.
and not very well know about pointers in C
A pointer is a variable which points to a specific address in the memory.
In this case, it points to the first letter of "world", which is then printed by printf() all until NUL (automatically inserted at the end of strings).
So, answering your question: yes, the output will be "world".