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 4 years ago.
Improve this question
I've seen some code that I can't figure out exactly what's going on there, the piece of code is: I got char *s1, char *s2 from the function, and they've done this:
*s1 = *s2;
What's going on behind the sence with this? (in C, asci-c)
In C and C++ * can be used with the pointer of a variable to access and assign its value.
Here:
*s1 = *s2;
*s2 is accessing the value of the variable that is pointed by s2 pointer and then assigning this value to the variable that is pointed by s1. While = is an assignment operator used to assign a value to a variable.
Please visit this site for complete reference.
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 5 years ago.
Improve this question
How can a structure's member (which is a pointer) be accessed through another pointer? Let's say that *ptr is the pointer i want to use to access *time, which is the pointer that belongs to the structure. Is it correct if I write ptr->time?
Would it be correct if I wrote ptr->time = v[i], if I wanted to assign the values of v[i] (array) to *time?
Would it be correct if I wrote ptr->time = v[i], if I wanted to assign the values of v[i] (array) to *time?
No. If you have...
struct {
int *time;
} *ptr;
int v[10], i = 0;
...then you have to write *(ptr->time) = v[i]
If time is a pointer, being inside a struct change nothing to that. So if you want to access the int pointed by time, you have to deference it too.
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
#include<stdio.h>
#include<string.h>
int main()
{
int i=0;
char *b,*a="a(a+b))";
b=a;
printf("%s",b);
printf("%d",a);
while(a[i]!='\0')
{
a++;
i++;
}
*a="(a+b)";
printf("%s",a);
}
initialy i am assigning a value to that char pointer
after moving it to the end using null comparision
can i again assign a value to that char pointer?
Can i do like this?
what is wrong here? can anyone please explain??
The reason your third line is failing is because youre trying to de-refrence and reassign a value to a "string" value on the stack. ( which is very very bad, please never do this if it ever lets you )
If you do want it to work, the compiler has to create a new string value and reassign the pointer so you would have to change the code to this.
a = "(a+b)";
This will create a new value on the stack and reassign the pointer to the beginning of that string.
However if you are going to use strings like that. PLEASE use const char *. Its ultimately safer and saves you a lot of headaches.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
char i[]=pop();
char j[]=pop();
b=atoi(i);
a=atoi(j);
I wanted to pop an char type element from stack and convert it to int type. But it says
invalid initializer.
What is the problem?
If you want a char variable, use a char variable, don't use a char array.
Change
char i[] = pop();
to
char i = pop();
and likewise.
That said, atoi() won't be relevant there. If you want the result to be of type int, simply use an int variable.
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
Here is the code:
#include <stdio.h>
int main ()
{
int c[4][4] = {{0,1,2,3},{4,5,6,7},{8,9,10,11},{12,13,14,15}};
int (*p)[3] = (int(*)[3])(c+2);
return 0;
}
Its interesting that it sets p to be:
{{8,9,10},{11,12,13},{14,15,-8224}}
I do not understand what is happening, especially in the 6th line of the code. Please help me!
int c[4][4] declares a 2D array with dimensions 4x4.
int (*p)[3] declares an array pointer to an array of 3 elements.
(int(*)[3])(c+2) invokes a pointer aliasing bug, by treating the address of c+2 as if there was an array of 3 elements there.
Pointer conversions between type int (*)[4] (the type of c+2) and type int(*)[3] are not safe. In practice, most compilers will likely give you some deterministic result from this code, but they are not required to do so. The program may as well crash and burn and then that's the programmer's fault.
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've seen certain functions declared in this way:
char* encipher(const char *src, char *key, int is_encode);
I don't understand this part:
char* encipher
What do the asterisks after the datatype mean?
This just means that the function returns a char *.
The asterisks after the data types mean that a pointer is expected, i.e.
char *src
means that src is a pointer to a char. Pointers are data types that contain addresses to instances of other data types, so a char* contains the address of a char. The first char* means that the function returns such a pointer.
But as others said, you may want to read a good textbook on C first.