0

I am trying to print a string read into the program from the user.

I need to be able to print the string by using a pointer.

Here is a simplified version of my code.

#include <stdio.h>
int main(void)
{
    char string[100];
    printf("Enter a string: ");
    scanf("%s", string);
    char *pstring = &string;
    printf("The value of the pointer is: %s", *pstring);
    return 0;
}

But I am getting a segmentation fault

Can someone please explain why this is happening?

2
  • *pstring is the value of the char that pstring is pointing to. Use pstring without dereferencing it. That is: pass the address contained in pstring to printf() not the first character of the user's string. The segfault is because printf has been handed rubbish and tries to find a string at some unpredictable address. Commented Oct 1, 2022 at 2:19
  • This should generate a compiler warning about mismatched types. If not, enable more warnings, especially -Wall. Commented Oct 1, 2022 at 2:34

2 Answers 2

1

You don't usually take an address of an array, it's either &string[0] or just string. In the printf() for %s you should pass a char pointer not a char (which is what *pstring is). It's a good idea to add a newline to the printf() format string as scanf() doesn't pass that along:

#include <stdio.h>

int main(void) {
    char string[100];
    printf("Enter a string: ");
    scanf("%99s", string);
    char *pstring = string;
    printf("The value of the pointer is: %s\n", pstring);
    return 0;
}
Sign up to request clarification or add additional context in comments.

Comments

1

string IS a pointer. A constant one.

It is worth understanding what arrays are (compared to pointers).

And specifically the difference between those:

char *str1=malloc(6);
strcpy(str1, "hello")
char str2[6];
strcpy(str2, "hello");

In this example, str1 and str2 are both pointers. But str1 is a variable (like "int x": x's value is stored in memory and you can modify it). Whereas str2 is not. It is a constant, like 12. It looks like a variable, because it is an identifier (str2), but it is not. Once compiled, there is no place in memory to store str2 value. The compiler computes its value, at compile time, and in the generated machine code, this value is inserted directly in the code. Exactly like for "12".

So str2=malloc(6) for example, or str2=whatever would make no sense. No more than typing 12=6 or 12=whatever. It is not a variable. It is a constant value.

And likewise, &str2 has no sense neither. No more than &12 has. It would be asking the address where a constant is stored. But a constant is not stored anywhere. It is a value directly in the code.

Namely str2 is a constant value of an address where the compiler has reseved enough room for 6 bytes. So you can store things in str2[0], str2[1], ..., str2[5], but not in str2 itself (again, it is a constant value, not a variable. You cannot store things in it, no more than you can store things in 12)

Situation for str1 is different. str1 is a variable, declared as type char *. You can store values of type char * in it (that is pointers to a char, that is memory address where a char is stored). And that declaration affect a default (initial) value to this variable, which is the address of 6 bytes, containing the letters 'h', 'e', 'l', 'l', 'o' and the terminal 0.

So, you can easily change str1, and store whatever pointer you want in str1. Writing str1=str2 for example.

So, long story short: both are pointers. str2 is a constant pointer, but still a pointer. Both are address of a place in memory where the first, then second, etc, char of the string are stored.

So, in your case, just type

char *pstring=string

or use string directly. Or, for aesthetic purpose, to clarify that you mean "address of the first char of string", you may type

char *pstring = &(string[0])

which is the exact same thing as "string" (in C, syntax a[b] is just a shortcut to *(a+b), so &(string[0]) is &(*(string+0)), which is &*string which is string (another strange consequence of that, is that you could as well say

char *pstring = &(0[string])

again, that just means &(*(0+string))

Note that I advise none of those strange writing. But it helps understand how pointers are just values, that you may store in variables (like with str1) or use directly as constant in the code (like with str2). It is just less obvious than for integers (x a variable declared as int x=12; vs 12 that a constant) because in case of arrays (constant pointers) those values are named by the compiler and take the concrete appearance of an identifier in the code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.