Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. adding to char arrays together for one arg

adding to char arrays together for one arg

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NZSmartie
    wrote on last edited by
    #1

    I've been using c++ for two weeks now and i've got a good grip on coding by myself but... is it possible to call a function like this: char* myFunc(char* input) { printf(input); } int main(void) { char* one = "hello "; char* two = "world!"; myFunc(one+two); } this is of the top of my head but can i add two strings together for a function? Even if there is away around this would be great. Thanks

    M I 2 Replies Last reply
    0
    • N NZSmartie

      I've been using c++ for two weeks now and i've got a good grip on coding by myself but... is it possible to call a function like this: char* myFunc(char* input) { printf(input); } int main(void) { char* one = "hello "; char* two = "world!"; myFunc(one+two); } this is of the top of my head but can i add two strings together for a function? Even if there is away around this would be great. Thanks

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      Roman957 wrote:

      but can i add two strings together for a function?

      No. There's not a + operator that will concatenate two char strings. You'd need to do that manually - here's one way:

      int main(void)
      {
      char* one = "hello ";
      char* two = "world!";

      char buffer[16];
      strcpy(buffer, one); // copy string "one" to buffer
      strcat(buffer, two); // append string "two" to buffer

      myFunc(buffer);
      }

      Mark

      "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

      1 Reply Last reply
      0
      • N NZSmartie

        I've been using c++ for two weeks now and i've got a good grip on coding by myself but... is it possible to call a function like this: char* myFunc(char* input) { printf(input); } int main(void) { char* one = "hello "; char* two = "world!"; myFunc(one+two); } this is of the top of my head but can i add two strings together for a function? Even if there is away around this would be great. Thanks

        I Offline
        I Offline
        Iain Clarke Warrior Programmer
        wrote on last edited by
        #3

        It may count as advanced at this stage of learning (getting arrays and pointers in your head is very handy!), but there are classes that encapsulate strings, and you can then use + eg: std::string one = "hello"; std::string one = "world"; somefunc (one + two); Also look at CString if you're using MFC. Iain.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups