Thread: Concat a Char to String.

  1. #1
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184

    Concat a Char to String.

    Hi,

    How to Concat a Char to String? to add in the last position of array char...

    Code:
    void SendToPC (unsigned char *pMsg){
    	
    	unsigned char *cMsgFinal;
    	int iLen;
    	
    	if (iUserSocket){
    
    		cMsgFinal [0] = cInicio;
    		strcat (cMsgFinal, pMsg);
    		//cMsgFinal [0] = cFinal; ??????????????
    		
    		iLen = strlen (pMsg);
    		
    		VcomSendSocket(iUserSocket, pMsg, iLen);	
    	}
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um... but it looks like cMsgFinal is just a pointer that points to nowhere in particular. You certainly cannot concatenate anything to it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    humm, and then

    Whats the better option ?

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    void SendToPC (unsigned char *pMsg){
      if (iUserSocket)
      {
        size_t iLen = strlen (pMsg);
    
        pMsg[iLen] = cInicio;
        pMsg[iLen+1] = 0;
    		
        VcomSendSocket(iUserSocket, pMsg, iLen);	
      }
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, you'd definitely need to have some memory for cMsgFinal, either by allocating a sufficient size to hold the final string (don't forget to free it when you no longer need it), or simply declaring cMsgFinal as an array that can hold the length of the final string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by sergioms
    Whats the better option ?
    It depends on what are you trying to do. Your given code does not provide enough information: it is not clear what are cInicio and cFinal, or which string should be concatenated to which string, and in fact cMsgFinal looks completely redundant because it is not used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    For arrays[] you must give the specific size that the array can possibly grow to be.

    For pointers you must set aside memory for the object it will point to by using malloc and then free it when done.

  8. #8
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Given that the parameter is not const I assume it is allowable for me to just be adding characters into it as opposed to making a second buffer. Though it makes no consideration for buffer overflow. But that is another issue altogether.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Code:
    void EnviaPedidoPC (unsigned char *pMsg){
    	
    	unsigned char cFinal [3];
    	unsigned char *cMsgFinal;
    	int iLen;
    	
    	if (iUserSocket){
    
    		cMsgFinal [0] = cSTX;
    		strcat (cMsgFinal, pMsg);
    		cFinal [0] = cCR;
    		cFinal [1] = cLF;
    		cFinal [2] = '\0';
    		strcat (cMsgFinal, cFinal);
    		
    		iLen = strlen (cMsgFinal);
    		
    		VcomSendSocket(iUserSocket, cMsgFinal, iLen);	
    	}
    }

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    cMsgFinal still points to "nowhere in partciular", which is not a good place to store things.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    ok, I will study Pointers

    I'm not understand

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    ok, I will study Pointers

    I'm not understand
    Yes, it seems like you do not understand pointers. A pointer is essentially a variable that holds the address of something memory. Until you have set it to point to something, it doesn't "hold" any memory.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Bloody hell. What are you wanting to do, exactly?

    Code:
    void EnviaPedidoPC (unsigned char *pMsg){
    	size_t iLen;
    	unsigned char cFinal [3];
    	unsigned char *cMsgFinal;
    
    	if(!pMsg)
    		return;
    
    	if (iUserSocket){
    		iLen = strlen (pMsg);
    		cMsgFinal = malloc(iLen + 3);
    
    		if(cMsgFinal)
    		{
    			//cMsgFinal[0] = cSTX;  <- what the f word is this?
    			strcpy(cMsgFinal, pMsg);
    			strcat (cMsgFinal, "\r\n");
    			VcomSendSocket(iUserSocket, cMsgFinal, iLen);
    			free(cMsgFinal);
    		}
    	}
    }
    Last edited by master5001; 11-20-2008 at 04:33 PM. Reason: Ooops. Bad Matt... since when do you copy and paste from a confused person's code!?

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    iLen = strlen (cMsgFinal);

    Uh-oh! calling function on not-initialized pointer - just asking for crash
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM