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.
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 1 year ago.
Improve this question
I like to know is this code wrong. reasons pleasec
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void func(char **c)
{
strcpy(c[0],"he");
strcpy(c[1],"h");
// memcpy(*c[2],"hell",sizeof("hell"));
// c[0]="hello";
//c[1]="hi";
}
int main()
{
char *arr[2];
arr[0]=malloc(sizeof(char[3]));
arr[1]=malloc(sizeof(char[2]));
func(arr);
printf("%s\n",arr[0]);
printf("%s\n", arr[1]);
return 0;
}
this line seems to allocate 3 * sizeof(char)
arr[0]=malloc(sizeof(char[3]));
this line seems to allocate 2 * sizeof(char)
arr[1]=malloc(sizeof(char[2]));
can array of pointers point to variable sized strings?
Yes.
is this code wrong.
func() is brittle in that it may exhibit undefined behavior depending on value of input.
No information about how many c[] may be indexed.
No information about how many bytes are available for strcpy(c[i], ....) to copy.
func() is OK in main() as it passes in a value that happens to not exceed the function limitations in OP's case.
func() is a wrong design in that is lacks parameters to guide its limitations and lacks null pointer error detection.
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.
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
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 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
Ok so I am curious as to how you would go about using a pointer array to access a value in the index. Like for example:
printf("%c", (*character)[0]);
I know I have this code wrong, but I have know clue how to fix it. Say I want to access the 0 position in the pointer array and then print it like above, how would I do it?
Assuming character is char character[] = {'1'};
character[someIndex] means someIndex[character] means *(character+someIndex)
If that is what you wanted to know. So you should be doing something like:
printf("%c", *(character+0));
Which is equivalent to
printf("%c", *character);
printf("%c", character[0]);
Just missed out - regarding this statement
index of a pointer array?
Please know Arrays are not Pointers. If that is where you were confused.
From your question I observe that you have declare a character pointer something like this
char *character="something";
and now you want access its contents by indexing.
So why worrying,No matter whether you have declare pointer array or array.You can access its contents by any of following way:-
printf("%c",character[1]); //general method
or by pointer notation
printf("%c",*(character+1));
or
printf("%c",*(1+character));//commutative law
or more surprisingly you can use following method too
printf("%c",1[character]);
char *arr[20]={"Stackoverflow"};
This means you have an char type pointer array where 20 memory location's addresses are stored in an 20 size array. 1st memory location in arr[0] points to the string "Stackoverflow" and rest of them are not assigned(so you will get garbage value or maybe segmentation fault). Now, if you want to access 0th memory location just do this:
printf("%s\n",arr[0]);