Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7196411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:46:22+00:00 2026-05-28T20:46:22+00:00

So, its a pretty simple problem and I know the solution which is a

  • 0

So, its a pretty simple problem and I know the solution which is a simple function like the one below:

  void removeSpaces(char* s) {
    char* source = s;
    char* dest = s;

    while(*source) {
        if(*source == ' ') {
            source++;
        } else {
            *dest++ = *source++;
        }
    }

    *dest = 0;
}

I am working in Visual C++ 2008 Express edition

When I call it with the following it works fine without any issues i.e it removes all the spaces:

int main() {
    char input[50] = "I       like            2% milk";
    removeSpaces(input);
    cout<<input;

    getchar();
    return 0;
}

But, the problem is when I call it by changing the string declaration to this:

char * input = "I       like            2% milk";

I get a exception (some kind of access violation)

The exception is showing on this line of code of the removeSpace function

*dest++ = *source++;

Can anyone elaborate as to why is this happening?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:46:24+00:00Added an answer on May 28, 2026 at 8:46 pm

    When you do

    char* something = "a string literal";
    

    The compiler puts "a string literal" into the executable image itself and just assigns a pointer to this memory to something. You are not allowed to modify this memory, and many times the memory that the string resides in is marked read-only, so any attempts to write to it result in an access violation like the one you experienced.

    When you do

    char something[] = "a string literal";
    

    you are really creating an array on the stack named something and initialising it with "a string literal". This is equivalent of doing char something[] = {'a', ' ', 's', 't', 'r', ..., 'a', 'l', 0};. Since this memory is on the stack, you can modify it freely.

    char* something = "a string literal" looks like

        stack                              executable
    
    -------------                     ---------------------
    |~~~~~~~~~~~|                     | ~~~~~~~~~~~~~~~~~ |
    | something | ----------------->  | a string literal0 |
    -------------                     | ~~~~~~~~~~~~~~~~~ |
                                      ---------------------
    

    Whereas char something[] = "a string literal" looks like

    stack
    
    -----
    |~~~|
    | a |  <- something is an alias for this location
    |   |
    | s |
    | t |
    | r |
    | i |
    | n |
    | g |
    |   |
    | l |
    | i |
    | t |
    | e |
    | r |
    | a |
    | l |
    | 0 |
    -----
    

    Where ~~~ means “other memory etc”.

    Note that

    char* x = "string literal";
    

    Is actually invalid and should not compile because you cannot convert a char const[x] to a char*. It should be const char* x, not char* x, but some old and non-conformant compilers wrongly allow this behaviour.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I like Gtalk's GUI very much. It's clear, simple, and pretty. I don't know
In Python, I'd like to write a function that would pretty-print its results to
I've put together a pretty simple competition script - it's commented out below and
I'm having trouble with what I thought should be a pretty simple problem. I
I've written a pretty simple Django application called django-locality which takes the headache out
I have built a pretty simple REST service in Sinatra, on Rack. It's backed
When writing manual SQL its pretty easy to estimate the size and shape of
Interview question- Often its pretty easier to debug a program once you have trouble
I've got a jPicker installed and running fine; its a pretty sweet script. However,
I'm trying to get this expression to work, I'm pretty sure its not the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.