Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. why strncpy not copy 4 character in c2 ?
Forum Updated to NodeBB v4.3 + New Features

why strncpy not copy 4 character in c2 ?

Scheduled Pinned Locked Moved Solved C++ Gurus
15 Posts 5 Posters 2.3k 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.
  • JonBJ JonB

    @Qt-embedded-developer
    Goodness, how does setting to c2 to NULL help? Do you not understand strncpy(), please read its documentation.....

    i have seen that when i use char array it show output what i expect. but why its not possible with char* ?

    Sorry, but you really need to read up on C basics....

    Further, you will see it does not put a terminating \0 into the destination area. If cout<<s2 prints something it would be hell followed by an unknown stream of random characters.....

    Q Offline
    Q Offline
    Qt embedded developer
    wrote on last edited by
    #6

    @JonB

    why this show correct output then ?

    char* c= "hello world",c2[10];
    memset(c2,0x00,sizeof(c2));
    strncpy(c2,c,4);

    cout<<c2;

    JonBJ 1 Reply Last reply
    0
    • Q Qt embedded developer

      @JonB

      why this show correct output then ?

      char* c= "hello world",c2[10];
      memset(c2,0x00,sizeof(c2));
      strncpy(c2,c,4);

      cout<<c2;

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #7

      @Qt-embedded-developer
      Precisely because that avoids all the faults previously mentioned in your code!

      char c2[10] allocates 10 bytes of storage which c2 "points to".

      char *c2 allocates no storage for c2 to point to. You would either need to malloc() space, or set c2 to point to, say, a char c3[10] which does allocate storage.

      You seem to think char c2[10] and char *c2 are "the same thing", which they are not.

      1 Reply Last reply
      2
      • JonBJ JonB

        @Qt-embedded-developer
        Goodness, how does setting to c2 to NULL help? Do you not understand strncpy(), please read its documentation.....

        i have seen that when i use char array it show output what i expect. but why its not possible with char* ?

        Sorry, but you really need to read up on C basics....

        Further, you will see it does not put a terminating \0 into the destination area. If cout<<s2 prints something it would be hell followed by an unknown stream of random characters.....

        Q Offline
        Q Offline
        Qt embedded developer
        wrote on last edited by
        #8

        @JonB can you give example to put \0 in above example code ?

        JonBJ 1 Reply Last reply
        0
        • Q Qt embedded developer

          @JonB can you give example to put \0 in above example code ?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #9

          @Qt-embedded-developer
          I really, really should not have to do this....

          char *c = "hello world";
          char c2[10];
          strncpy(c2, c, 4);
          c2[4] = '\0';
          cout << c2 << endl;
          

          One further thing: these days, using C++ (and also Qt), you really should no longer need to use C library functions like strncpy() (or malloc()). There are better, safer ways to do that, e.g. using C++ std::string or Qt QString (and new for malloc()). Same with e.g. QByteArray instead of memcpy/set().

          Q 1 Reply Last reply
          1
          • JonBJ JonB

            @Qt-embedded-developer
            I really, really should not have to do this....

            char *c = "hello world";
            char c2[10];
            strncpy(c2, c, 4);
            c2[4] = '\0';
            cout << c2 << endl;
            

            One further thing: these days, using C++ (and also Qt), you really should no longer need to use C library functions like strncpy() (or malloc()). There are better, safer ways to do that, e.g. using C++ std::string or Qt QString (and new for malloc()). Same with e.g. QByteArray instead of memcpy/set().

            Q Offline
            Q Offline
            Qt embedded developer
            wrote on last edited by Qt embedded developer
            #10

            @JonB said in why strncpy not copy 4 character in c2 ?:

            char *c = "hello world";
            char c2[10];
            strncpy(c2, c, 4);
            c2[4] = '\0';
            cout << c2 << endl;

            sorry for not clearly say about code. i said about below code.

            can you just let me know what change in below code i need to do to add '\0'.

            char* c= "hello world",*c2;
            c2= NULL;
            memset(c2,0x00,sizeof(c2));
            strncpy(c2,c,4);

            JonBJ jsulmJ 2 Replies Last reply
            0
            • Q Qt embedded developer

              @JonB said in why strncpy not copy 4 character in c2 ?:

              char *c = "hello world";
              char c2[10];
              strncpy(c2, c, 4);
              c2[4] = '\0';
              cout << c2 << endl;

              sorry for not clearly say about code. i said about below code.

              can you just let me know what change in below code i need to do to add '\0'.

              char* c= "hello world",*c2;
              c2= NULL;
              memset(c2,0x00,sizeof(c2));
              strncpy(c2,c,4);

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #11

              @Qt-embedded-developer
              No, I'm sorry, I have reached my limit as to what I consider suitable to write/explain for you on this Qt site. For your own good you need to (a) read up about basic C, (b) understand code and (c) write/adapt your own for really simple stuff instead of asking someone else to do it for you.

              • You have ignored what I said about c2 = NULL, and any understanding of allocating space.
              • You have not made any attempt to look at what I did write for you and plainly/easily apply it to your new case.
              • You have written (copied?) memset(c2,0x00,sizeof(c2)); and not understood how that would affect any need to "add '\0'.".
              1 Reply Last reply
              7
              • Q Qt embedded developer

                @JonB said in why strncpy not copy 4 character in c2 ?:

                char *c = "hello world";
                char c2[10];
                strncpy(c2, c, 4);
                c2[4] = '\0';
                cout << c2 << endl;

                sorry for not clearly say about code. i said about below code.

                can you just let me know what change in below code i need to do to add '\0'.

                char* c= "hello world",*c2;
                c2= NULL;
                memset(c2,0x00,sizeof(c2));
                strncpy(c2,c,4);

                jsulmJ Offline
                jsulmJ Offline
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #12

                @Qt-embedded-developer said in why strncpy not copy 4 character in c2 ?:

                can you just let me know what change in below code i need to do to add '\0'.

                YOU NEED TO ALLOCATE MEMORY FOR c2...
                You was already told so here: "You would either need to malloc() space, or set c2 to point to, say, a char c3[10]".
                Please try at least to understand what others are writing.

                As others already pointed out: learn C/C++ basics.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                Q 1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Qt-embedded-developer said in why strncpy not copy 4 character in c2 ?:

                  can you just let me know what change in below code i need to do to add '\0'.

                  YOU NEED TO ALLOCATE MEMORY FOR c2...
                  You was already told so here: "You would either need to malloc() space, or set c2 to point to, say, a char c3[10]".
                  Please try at least to understand what others are writing.

                  As others already pointed out: learn C/C++ basics.

                  Q Offline
                  Q Offline
                  Qt embedded developer
                  wrote on last edited by Qt embedded developer
                  #13

                  @jsulm @JonB Thank you

                  i got the success:

                  char* c= "hello world",*c2;
                  int n=5;
                  c2= NULL;

                  c2 = (char*) malloc(n*sizeof(char));
                  strncpy(c2,c,4);

                  cout<<c2; // this print the hell

                  JonBJ 1 Reply Last reply
                  0
                  • Q Qt embedded developer

                    @jsulm @JonB Thank you

                    i got the success:

                    char* c= "hello world",*c2;
                    int n=5;
                    c2= NULL;

                    c2 = (char*) malloc(n*sizeof(char));
                    strncpy(c2,c,4);

                    cout<<c2; // this print the hell

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #14

                    @Qt-embedded-developer
                    Yeah, but why in the world you need such code with strncpy() & malloc() is quite beyond me....

                    P.S.
                    Technically your code is still not right. You will only copy hell, 4 characters, into the malloc()ed area, and that does initialize its memory allocation. The 5th character onward could be anything. You are "lucky" the cout<<c2 does not print helljgas978623i5tagsd534... :)

                    strncpy(c2,c,4);  // copies "hell", 4 characters, nothing more, does not add any terminator
                    c2[4] = '\0';  // need terminating '\0' here
                    cout<<c2; // this print the hell --- FOR SURE!
                    
                    S 1 Reply Last reply
                    4
                    • JonBJ JonB

                      @Qt-embedded-developer
                      Yeah, but why in the world you need such code with strncpy() & malloc() is quite beyond me....

                      P.S.
                      Technically your code is still not right. You will only copy hell, 4 characters, into the malloc()ed area, and that does initialize its memory allocation. The 5th character onward could be anything. You are "lucky" the cout<<c2 does not print helljgas978623i5tagsd534... :)

                      strncpy(c2,c,4);  // copies "hell", 4 characters, nothing more, does not add any terminator
                      c2[4] = '\0';  // need terminating '\0' here
                      cout<<c2; // this print the hell --- FOR SURE!
                      
                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #15

                      @JonB said in why strncpy not copy 4 character in c2 ?:

                      Technically your code is still not right.

                      Also: There is a memory leak because there is no matching free() for the malloc().

                      Just because it works now, does not mean it is valid code. You need to make sure for every C-style string that there is a terminating '\0'. There is no other way to know the length of the string. Every C algorithm expects the terminating '\0'. The reason why this might work right now could be that you are compiling a debug version and this will initialize the memory with zeros. Once you compile a release version your application might suddenly crash. At least it is not guaranteed to work. Another reason that it works might be because you are lucky that there is a 0 in memory. Just one little change in code might get you a different memory allocation/access pattern and the 0 is gone.

                      Since you are already using cout this means you are using C++. C++ is here to help you, not to punish you. Use std::string instead of char*. Try to avoid new and malloc because you always might forget the corresponding delete or free. std::string and std::vector (and other containers) will gladly manage memory for you. If you really need a pointer, use a smart pointer (together with make_unique or make_shared, so you don't have to use new which might accidentally leave a memory leak).

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved