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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:06:05+00:00 2026-05-31T06:06:05+00:00

I want to do something like looks like this (obviously not valid c code,

  • 0

I want to do something like looks like this (obviously not valid c code, though):

char test[] = "you";
char new[] = "hey %s over there", test; // Want to get back "hey you over there"

Here’s my way, but it seems too complicated. Get the len of both the test and new, create a new string buffer that can hold both lengths, concatenate new and test to the string buffer. Is there a better way to do this?

Also char hi[] = "hi" vs char *hi = "hi". What’s the difference?

  • 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-31T06:06:07+00:00Added an answer on May 31, 2026 at 6:06 am

    Well, that’s the time-honored traditional way of doing it in C, though I think you meant the length of test and the string literal since new doesn’t have a length until you write the data into it.

    And, as an aside, if you ever expect that your code will be used in a C++ compiler, please don’t call your variables new 🙂 Although I’m assuming that was just a quick and dirty code sample since new isn’t a good descriptive name for a variable anyway (newString, or something similar, would be better).

    If you’re likely to be doing it a lot, there’s no problem with writing a helper function that does all the grunt work for you. That may make your code look cleaner but I’d simply go for your current solution using strcpy/strcat/strlen/sprintf et al.

    Or, you can use a third-party piece of code like the better string library, licences and management attitudes permitting (the licences are BSD/GPL but management can still sometimes be a problem).

    It has the advantage of not dragging in a lot of extraneous functionality as some third-party libraries are wont to do. All it does is the string handling.


    As to the difference between these two:

    char hi[] = "hi";
    char *hi = "hi";
    

    the first gives you a modifiable character array and the latter does not (try hi[0] = 'a'; in both cases, doing so with the latter would be undefined behaviour). The latter also allows you to change the value of the hi pointer to point somewhere else.

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

Sidebar

Related Questions

I want to have a route that looks something like: www.abc.com/companyName/Controller/Action/Id However, all the
I want to have a template class that looks something like what I have
i want something like this the user enter a website link i need check
I want something like this: <msxsl:script language=C#> ??? getNodes() { ... return ... }
I have a git repository set up that looks something like this: PROD /
I have some MET data I want to validate which would look something like
I want something like an std::map , but I only want to see if
I want something like a canvas, but where i'd be able to manipulate pixels
:bn , :bp just switches buffers I want something like ctrl-o, which has a
Say I have the classic 4-byte signed integer, and I want something like print

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.