I want to display "string pointer affected" but I get an error. Here is my code:
#include<stdio.h>
main()
{
char* *p;
char * s="string pointer affected";
*p=s;
printf("%s",*p);
}
p doesn't point to any known location, so writing to *p is a bad idea.
You mean to say:
p = &s;
You dereference a pointer which is not initialized , which will cause undefined behaviour . This is problem -
*p=s;
You are using an uninitialized variable in the line below and in the printf statement. If you replace
*p = s;
with
p = &s;
then it will work.
Try:
#include<stdio.h>
main()
{
char *p; // <--------------------------
char *s="string pointer affected";
printf("===== s=%p\n", s);
p=s;
printf("===== p=%p\n", p);
printf("%s\n", p);
}
The problem with the original code is that p is uninitialized. So you cannot dereference it.
If you do want to use a pointer to a pointer, allocate the pointer first, and then take its address.
#include<stdio.h>
main()
{
char *q;
char **p = &q;
char *s="string pointer affected";
*p=s;
printf("%s\n", *p);
}
Related
The method signature for getenv
char *getenv(const char *name)
The return value is a pointer to char .
Now if we look at the below example :
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *p;
p = getenv("PATH");
if(p != NULL)
printf("Current path is %s", p);
return 0;
}
The only confusion here is the "p" printed with format specifier "%s" as String but what about the Pointer Dereference
If I put "*p" instead, I end up with segmentation fault
while if I want to get the pointer address I can do this
printf("%p", p);
confusion about getting the value by deferencing the point "p" in our case.
Please explain
Looks like you're confused about some fundamental basics in C.
In case of
char *p;
p is a pointer to a series of char, which is in fact a \0 terminated
string. This string can be printed with printf("%s", p);
Since p is a pointer you can print the memory address it points to printf("%p", p);, which is exactly the same as if p was defined as void *p;
*p dereferences the first char in the string pointed to by p, which is essentially the same as p[0]. This string can be printed with printf("%c", *p); or printf("%c", p[0]);
I am learning c, a beginner, can anybody please make me understood which concept am I missing?
And thanks in advance.
#include<stdio.h>
int main()
{
char s[10];
s[0]='A';
s[1]='B';
s[2]='\0';
char *p;
int i;
p=s;
printf("%c\n", *p); //It's ok.
printf("%s", *p); // or *s...what's wrong here,why does program crash?
return 0;
}
Change
printf("%s", *p);
to
printf("%s", p);
The reason why is that %s is expecting a pointer, and *p is the dereferenced value at p, aka the char value at p[0].
If this doesn't make sense, picture why printf("%c\n", *p) works. *p is the same as p[0], which is the same as s[0] since p points to s. Because s[0] is a char, %c works here because it is expecting a char. But %s on the other hand expects char *.
You want printf("%s", p). Don't dereference the pointer.
#include <stdio.h>
#include <string.h>
void replace (char a[]){
char *y;
*y = 'm';
char *p = a;
p = strchr(p, 'g');
while (p){
*p = *y;
p++;
p = strchr(p, 'g');
}
}
int main (){
char x[10];
gets(x);
replace(x);
puts(x);
return 0;
}
What's wrong with this replace function?
It doesn't output a string instead it says segmentation fault.
You're trying to write to a wild pointer here:
char *y;
*y = 'm';
y doesn't point anywhere in particular, so you get Undefined Behaviour (a seg fault in your particular case).
You are assigning value using an uninitialized pointer, y.
Why do you use pointer y anyway, instead of
*p = *y;
you can just say
*p = 'm';
y is not allocated. It is just a pointer and a pointer must point to a space in the memory. But you didn't allocate any space in the memory. So when you defferentiate it, is going to deferentiate the garbage address that a non itialized pointer has. Crash...
So rather than
char *y;
*y='p';
just write:
char y='p'; ///no pointer
Then a first improvment at the function. The function is too specific, just for a character, I would write like:
void replace (char a[],char from, char to)
{
char *p = a;
while(*p)
{
if(*p==from) *p=to;
p++;
}
}
If you compile your program with warnings enabled you should get a warning like this (with the GCC compiler):
warning: ‘y’ is used uninitialized in this function [-Wuninitialized]
*y = 'm';
^
Before you dereference the pointer y you need to know that it points to a valid object, but in your case y has not been assigned a value so it can point anywhere. Also you don't need any extra pointer; here is a more concise (and more general) version of the function:
static void replace(char old, char new, char s[])
{
s = strchr(s, old);
while (s != NULL) {
*s = new;
s = strchr(s, old);
}
}
Or without using strchr:
static void replace(char old, char new, char s[])
{
int i = 0;
while (s[i] != '\0') {
if (s[i] == old) {
s[i] = new;
}
i++;
}
}
please advise me on the following output:
int main()
{
char ***x = "jjhljlhjlhjl";
char ***q = "asddfwerwerw";
**q = **x;
printf("x:%s\n",x);
printf("q:%s\n",q);
}
Output: 1 Segmentation fault
This is what you should have:
#include <stdio.h>
int main(void) {
char *x = "jjhljlhjlhjl";
char *q = "asddfwerwerw";
q = x;
printf("x:%s\n",x);
printf("q:%s\n",q);
return 0;
}
If you want to initialize a character string, use char *x
Don't use ***x. That means pointer to pointer to pointer to a char.
Hope that helps.
"Segmentation fault" is not an output, it's an indication that your program has crashed.
This should come as no surprise, because string literals are char*, not char***. Trying to double-derefefence such pointers is undefined behavior, because it re-interprets the content of a string literal as a pointer to char. This is what is causing the crash.
You can modify your program as follows to make it legal:
int main() {
char *x = "jjhljlhjlhjl";
char tmp[] = "asddfwerwerw";
char *q = tmp;
*q = *x;
// This will produce an output that should be easy to explain:
printf("x:%s\n",x);
printf("q:%s\n",q);
}
Hi please kindly explain to me why is the code generating an error,
#include<stdio.h>
int main(){
char ***x;
char **q = *x;
char **(*c) = x;
char ***d = &q;
char ***p = "asdasd";
x=p;
printf("d:%s\n",d);
printf("q:%s\n",q);
printf("x:%s\n",x);
return 0;
}
Output:
1
Segmentation fault
Hi Thanks for the replys so if I init x, i still got an Segmentation fault on
printf("q:%s\n",q);
the output and code is shown below, please kindly advise why is d:1231 instead of 1231123124 and why x=p only change the value of x instead of all (x, q, d)
int main(){
char ***x = "1231123124";
char **q = *x;
char **(*c) = x;
char ***d = &q;
char ***p = "asdasd";
x=p;
printf("p:%s\n",p);
printf("d:%s\n",d);
// printf("q:%s\n",q);
printf("x:%s\n",x);
printf("c:%s\n",c);
return 0;
}
Output:
p:asdasd
d:1231
x:asdasd
c:1231123124
char **q = *x;
Here you are dereferencing an uninitialized pointer.
It's undefined behaviour, in this case it usually results a segfault (in practice your code will try to dereference some random memory location, or NULL if your compiler initializes local variables (this is typical for debug/non-optimized builds)).
These are type errors.
printf("d:%s\n",d); // d is char***, not char*
printf("q:%s\n",q); // q is char**, not char*
printf("x:%s\n",x); // x is char***, not char*
The %s specifier expects a char * argument, or possibly void *, const char *, etc., but never a char ** or char ***.