Why and when to use pointers in C? [closed] - arrays

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 9 months ago.
Improve this question
I'm still very new to C, have only been learning for about a week at this point, but something I don't get is the use of pointers. I get how pointers work and I know how to use them, but I don't see why or when they should be used. Can someone please explain this to me?

It is enough to mention that arrays used in expressions with very rare exceptions are implicitly converted to pointers to their first elements.
For example if you will write the expression
arr[i]
where arr is the array name then even in this expression the array is converted implicitly to a pointer to its first element.
Or another simple example: all C string functions deal with pointers of the type char * because passed arrays again are implicitly converted to pointers to their first elements.
Think about how to write a general sort function for arrays of any types.

Related

Converting data to code [closed]

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 4 years ago.
Improve this question
In C, I'd like to make a macro called TEST() that takes a valid C arithmetic expression and prints and evaluates it. So as an example, if I were to give it TEST(5*2+3) it would print to console 5*2+3 = 13. Unfortunately, I don't know how to convert an expression to a string nor do I know how to take a given string and evaluate it as code. How would I do this?
You can use the stringification operator to turn the argument into a string, then expand into a printf() call that prints the string and the result. My code assumes that the expression is always an int.
#define TEST(EXP) printf("%s = %d\n", #EXP, (EXP))
For the parsing part of your question:
The Shunting-yard algorithm is what you're after.
It is the most common way (that I know of) to convert a mathematical expression represented by a string of characters into a postfixed version(operators come after the operands instead of between), which is much easier to interpret with code.
Good luck!

Can a function return a string in c? [closed]

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 7 years ago.
Improve this question
Question is pretty much self-explainable, but I will add an example just to clarify it.
For example, we have this function:
char abc(void)
{
code...
code...
return a;
}
This function will return char a to abc, but can a function return a whole string, without using pointers, file I/O and such things?
C strings are arrays of char, terminated by a char with value 0. C functions cannot return arrays themselves, including strings, nor, indeed, can arrays be passed as arguments. Passing and / or returning pointers is C's alternative to passing and / or returning arrays. To a large extent you don't even need to do anything special to pass or return pointers in place of arrays.
Because it is somewhat cumbersome to speak precisely about these things, the pointer / array distinction is often left out of discussion and even documentation. One often refers to a char * as a string, when in fact the string is the array whose first element is pointed to. This does present the possibility of confusion, however, as is evident in the question and the associated comments.

Why does C use the asterisk to reference the value of a pointer? [closed]

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.

Loop thorough **variable in C [closed]

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.

why we use function returning pointer in c and application of function returning pointer [closed]

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 9 years ago.
Improve this question
What is the main reason to use function returning pointer? Basically its return the pointer value but there are we can also return the value through pointer.
So why we need function returning pointer?? Is it reduce the size of code or any else?
Is there any application where we use function returning pointer?
Not sure what you are asking but yes? you could copy a 2Gb variable or an address to that variable that is orders of magnitude smaller. You tell me which is quicker/more efficient ;)
Here is a tutorial that explains their usage:
C pointers
Right off the bat it explains how it is easy to use numbers to lookup a safety deposit box so you can go access it.
without the ability to return pointers there would be no point to even having pointers at all. i.e. when you allocated memory for data, a function returns the pointer to its location. So without that you would never know were the memory you allocated is.
So I guess the functionality of returning pointers is arguably to allow the presence of pointers themselves?
One example, you can return a pointer to a value. Anyone who has that pointer can then change the value.

Resources