سؤال

I am pretty new to C so maybe someone can shed some light on why I am getting a segmentation fault with this program:

#include <stdlib.h>
#include <stdio.h>

int main()
{
    char *username = "hello";
    char *other;
    *other = *username;

    return 0;
 }

Why is this seg faulting?

هل كانت مفيدة؟

المحلول

other hasn't been initialized. It's pointing into some random spot in memory, and you're sticking a character in there.

You'll want to either allocate some space for other:

char *other = malloc(6);  // or 1, or however much space you'll eventually need
*other = *username;  
// first char at `other` is now 'h'; the rest is unknown. To make it valid string,
// add a trailing '\0'
other[1] = '\0';

or if you're trying to make a duplicate string:

char *other = strdup(username);

or if you're trying to make other point to the same place as username:

char *other = username;

نصائح أخرى

"Okay as I understand it char *username = "hello"; 
 automatically allocates a place for the string."

It might be helpful to better define the word 'it' (as used in the above sentence); which is 'the compiler' (without thinking too deeply about the linker and loader). This means that the compiler prepares a place for the static string "hello" before the program is executed. For example, you can 'grep' the executable and discover that the executable contains the string 'hello'.

As the program is loaded into memory by the OS, a 'heap' is established where the program may allocate memory dynamically.

The static string "hello" is not part of the heap; and was not 'allocated' dynamically at run-time. Rather, it was 'allocated' statically at compile time.

A static value, such as the string "hello", cannot be resized by 'realloc()', or freed by 'free()' because these functions only apply to items allocated from the 'heap', not static "compile-time" allocations.


The following code declares a static string "hello". It also declares a character pointer 'username', and initializes the pointer to point to the static string.

char *username = "hello";

The following code declares an character pointer 'other', and leaves it uninitialized. Being that 'other' is uninitialized, and that pointers are always pointing somewhere; it is likely that the uninitialized 'other' pointer is pointing at a random memory location (which does not actually exist).

char *other;

The following line attempts to copy one character ('h') from where username is pointing, to where 'other' is pointing (a random memory location which does not actually exist).

*other = *username;

This is what generates the 'Segmentation Fault'.


FYI, the following statements would cause the pointers 'username' and 'other' to point to the same place, the static string "hello":

char *other;
other = username;

...which is the same as:

char *other = username;

If you like, you can cause "hello" to be allocated from the heap (at run time) as follows:

char *username = strdup("hello");

If you would like another copy in 'other':

char *other = strdup("hello");
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top