0

I've spent hours scouring the internet for help. I'm very much a beginner at pointer use, and I've already come up against a wall: I keep getting the error Segmentation fault (core dumped). I'm trying to make a simple version of strncpy() using pointers:

int main(int argc, char *argv[]) {
   char *x = "hello";                   /* string 1 */
   char *y = "world";                   /* string 2 */
   int n = 3;                           /* number of characters to copy */

   for (int i=0; i<=n; i++) {
      if(i<n) {
         *x++ = *y++;                   /* equivalent of x[i] = y[i] ? */
         printf("%s\n", x);             /* just so I can see if something goes wrong */
      } else {
         *x++ = '\0';                   /* to mark the end of the string */
      }
   }
}

(Edit: I initialized x and y, but still got the same fault.)

While on the quest to figure out what part of this was wrong, I tried another simple pointer thing:

int main(int argc, char *argv[]) {
   char *s;
   char *t;
   int n;                               /* just initilaizing everything I need */

   printf("Enter the string: ");
   scanf("%s", s);                      /* to scan in some phrase */
   printf("%s", s);                     /* to echo it back to me */
}

And lo and behold, I got another Segmentation fault (core dumped)! It let me scan in "hello", but replied with the fault. This code is so simple. What's wrong with my pointer use here?

Kai C-m
  • 23
  • 4

2 Answers2

0

In your second example, you don't actually allocate any memory. char *s only allocates a pointer to a char. You need to allocate memory somehow:

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

int main(int argc, char *argv[]) {
   char s[100];
   printf("Enter the string: ");
   scanf("%s", s);                      /* to scan in some phrase */
   printf("%s", s);                     /* to echo it back to me */
}

char s[100] declares memory on the stack, which will be deallocated automatically. If you'd like to allocate on the heap, use malloc / free:

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

int main(int argc, char *argv[]) {
   char *s = malloc(100 * sizeof(char));
   printf("Enter the string: ");
   scanf("%s", s);                      /* to scan in some phrase */
   printf("%s", s);                     /* to echo it back to me */
   free(s);
}

Of course these simple examples assume your string will never be longer than 100 characters.

Your first example fails too, for a different reason.

   char *x = "hello";
   char *y = "world";

Those statements allocate strings in read-only memory, and thus you cannot modify it.

bejado
  • 1,430
  • 12
  • 19
  • Thank you! I'm guessing I should allocate some memory to it using malloc? Also, how could I make them writeable? – Kai C-m Mar 25 '17 at 21:58
  • @KaiC-m You can either use `malloc` or declare an array of `char`s on the stack. If you use `malloc` be sure to `free` the memory afterwards! – bejado Mar 25 '17 at 22:02
0

When you are using pointer to string, always rember that you cant modify it. It means you cant change the string characters. In Pointer to string, string always goes to read only memory.It mean memory can only be read not to modify. This statement is causing segment fault;-

*x++ = *y++;

you cant do this also;-

int *p="cool";
 *p="a";         //dereferencing 
printf("%s",p); //segment fault