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 5993635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:42:11+00:00 2026-05-22T23:42:11+00:00

I am new to C, and I am having difficulty understanding the reason why

  • 0

I am new to C, and I am having difficulty understanding the reason why the block of code below is not working.

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

int main()
{
    char *src = "http://localhost";

    /* THIS WORKS
       char scheme[10];
       char *dp = scheme;
     */

    //DOESN'T WORK
    char *dp = malloc(10);

    while (*src != ':') {
        *dp = *src;
        src++;
        dp++;
    }
    *dp = '\0';

    /* WORKS
       puts(scheme)
     */

    //DOESN'T WORK
    puts(dp);
}

The expected output is http to stdout. In both cases dp should be a pointer to an array of char pointers (char **). However it is printing nothing when using malloc method. I ran the code through GDB and my src and dp are getting erased 1 character at a time. If I enclose the while loop into a function call it works. I figured that the reason was because parameters are evaluated as a copy. However, then I read that arrays are the exception and passed as pointers. Now I am confused. I can work around this, but I am trying to understand why this way doesn’t work.

  • 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-22T23:42:12+00:00Added an answer on May 22, 2026 at 11:42 pm

    You are changing dp inside the loop

    dp = malloc(10);
    

    let’s say dp has the value 0x42000000

    while () {
        dp++;
    }
    

    let’s say the loop went 4 times, so dp has the value 0x42000004

    *dp = 0;
    

    now you put a null character at the address pointed to by dp

    puts(dp);
    

    and then you try to print that null 🙂

    Save dp and print the saved value

    dp = malloc(10);
    saveddp = dp;
    /* ... */
    puts(saveddp);
    free(saveddp); /* for completeness */
    

    It works with scheme because scheme is an array and you cannot change that address!

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

Sidebar

Related Questions

I am new to programming and am having difficulty with understanding object lifetime /
Intro I am new to GTK+ programming and am having difficulty understanding why the
I seem having difficulty in understanding the reason behind the need of having many
I was given this starter code for a project, but am having difficulty understanding
I've inherited the following Perl code, and I'm having difficulty understanding what exactly the
I'm new to Objective-C and I am having some difficulty with understanding memory management.
So I'm new to regular expressions and having difficulty understanding them. To get my
I'm having difficulty replacing the text of a link with new text. I have
Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here
I am having some difficulty understanding how containers are implemented in C++. Specifically, how

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.