In my code, I have declared a string var c, for holding the . After adding the key, when i access a particular character using c[i], it gives me a 'segmentation fault (core dumped)'.

I was able to solve this by making a change in the input string itself. But why couldn't I access individual characters of the empty string?


When we declare a string and initialize it with a string literal like

string s = "kray";

this string is stored in a read-only memory location. And that's why you get a segmentation fault when you then try to access the characters of this string and change their values.


apparently, I misunderstood your question. I didn't notice that you were talking about an empty string.

I assume that's what you mean by an empty string

string s;

Well, a string variable, unlike other types of variables (e.g., char and int), is little bit different in a way that's it's actually an array of characters underneath the hood.

You'll know more about that later on in the course, but for you not to be confused, you should be informed that we need to know the size of the string beforehand in order to reserve a big enough block of memory to store it on unless you're initializing it with a string literal (or an initializer list in case of a char array).

When you declare a string like s the way we did up here then you try to access a char in that it like s[0], you get a segmentation fault because we didn't really ask our computer yet to reserve a block of memory to store that string on, so our computer assumes that no memory reserved for that and you're trying to access something that should really be accessed yet.

A simple way to create an empty editable string is to use a char array. For example,

char str[size+1]; // empty string

where size is the number of characters in that string and the +1 is for the null terminator (i.e., '\0').


Segmentation fault occurs when you try to access a value from a memory location which is set to NULL or simply we can say that the memory block is empty.

For eg.

#include <iostream>
using namespace std;

int main() 
    string s = "Hello, World!";
    cout<<s[12]<<endl;     //outputs '!'
    cout<<s[13]<<endl;     //results in segmentation fault as s[13] doesn't store any value
    cout<<s[14]<<endl;     //results in segmentation fault as s[14] doesn't store any value
    return 0;

Check whether in your code, you aren't randomly accessing the array(char array) with a index out of the range of the size of array.

  • I had declared the var as string c=NULL; If that gives an error, how do we define an empty string so that we can access the characters?
    – kray
    Jul 15 '14 at 13:41
  • Do you wish to ACCESS the elements of an empty string? What value do you expect them to hold?
    – sinister
    Jul 15 '14 at 14:02
  • I added the key to the plaintext and stored the char p[i]+key into c[i]. However, since I initialized c with null, this gives me a segmentation fault. So how should I declare and initialize c?
    – kray
    Jul 15 '14 at 14:45
  • datatype variable_name [number_of_elements_in_this_array]; For eg. int x[10]; or char ch[20]; or char* c; // in such ways.
    – sinister
    Jul 15 '14 at 14:57
  • Yeah. But since the user will enter the plaintext, I won't know beforehand how many spaces to allocate. So should I allocate, say, 50? And if I do this, how do I print the ciphertext only till the number of char entered by the user and not the garbage values after that?
    – kray
    Jul 15 '14 at 15:03

Thank you @sinister and @Kareem . This problem had an easy fix. Use a char array after taking the input from the user.

string p=GetString();
int x=strlen(p);
char c[x];

This way, you can access characters of the string.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .