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

  • SEARCH
  • Home
  • 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 320653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:46:24+00:00 2026-05-12T08:46:24+00:00

As said in the title, the goal is to copy a C-style string into

  • 0

As said in the title, the goal is to copy a C-style string into memory without using any standard library functions or subscripting.

Here is what I have so far [SOLVED]

#include "std_lib_facilities.h"

char* strdup(const char* p)
{
    int count = 0;
    while (p[count]) ++count;
    char* q = new char[count+1];
    for (int i = 0; i < count + 1; ++i) *(q + i) = *(p + i);
}

int main()
{
    char word[] = "Happy";

    char* heap_str = strdup(word);
}

Obviously the problem is that allocating just *p (which is equivalent to p[0]) only allocates the letter "H" to memory. I’m not sure how to go about allocating the C-style string without subscripting or STL functions.

  • 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-12T08:46:24+00:00Added an answer on May 12, 2026 at 8:46 am

    C-style string ends with ‘\0’. You need to traverse the string inside the function character by character until you encounter ‘\0’ to know how long it is. (This is effectively what you would do by calling strlen() to work it out.) Once you know how long the string is, you can allocate the right amount of memory, which is the length+1 (because of the ‘\0’).

    To access the i’th element of an array p, one use subscript: p[i].

    Subscript of the form p[i] is formally defined to be *((p)+(i)) by both the C standard (6.5.2.1 of C99) and the C++ standard (5.2.1 of C99). Here, one of p or i is of the type pointer to T, and the other is of integral type (or enumeration in C++). Because array name is converted automatically (in most types of use anyway) to a pointer to the first element of said array, p[i] is thus the i’th element of array p.

    And just like basic arithmetic, ((p)+(i)) is equivalent to ((i)+(p)) in pointer arithmetic. This mean *((p)+(i)) is equivalent to *((i)+(p)). Which also mean p[i] is equivalent to i[p].

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

Sidebar

Related Questions

As said in the title i want to stop Hibernate console output, without using
As the title said, or can I just use them directly without any declaration?
As the title said: Is it possible to send an email (using php mail())
I am having trouble doing what the title said. My goal is to be
as title said, Im trying to change only the first occurrence of word.By using
As the title said I am bringing in a date string from a datatable
As the title said, I need to format a string of text in format
like said in the title I'd like to change a button's backgroun (using setBackgroundResource()
As already said in the title, how do i export a table into Excel
as i said in the title: i am using Java, but when I run

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.