Thread: Fill Array char with 40 spaces.

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

    Fill Array char with 40 spaces.

    How the best form to Fill a Array Char, with 40 spaces ?

    Im using this (bold in code) to concat, but I m afraid this not a good idea, because the system have no sufficient memory and processor to execute this rotine all times.

    somebody, can say a better c ode ?

    Code:
    void EscreveDisplay (unsigned char *pMensagem, unsigned int pDelaySec){
    	unsigned char cDispPacote [52];
    	int iComDisplay = 2;
    	unsigned int iLims;
    	unsigned int iLenMsg, iLenCont, iContIndex;
    	int iBcc;
    	
    	iDisplaySend = 1;
    	if (pDelaySec > 0)
    		Delay(pDelaySec * 1000);
    	
    	iLims  = 46;
    	
    	cDispPacote [0] = 129;
    	cDispPacote [1] = 126;
    	cDispPacote [2] = iLims % 256;
    	cDispPacote [3] = iLims / 256;
    	cDispPacote [4] = 1 % 256;
    	cDispPacote [5] = 1 / 256;
    	cDispPacote [6] = 0 % 256;
    	cDispPacote [7] = 0 / 256;
    	cDispPacote [8] = 76;
    	cDispPacote [9] = 202;
    		
    	iLenMsg = strlen (pMensagem);
    
    	if (iLenMsg > 40)
    		iLenMsg = 40;
    
    
    	if (iLenMsg < 40){
    		for(iLenCont=iLenMsg; iLenCont<40; iLenCont++)
    			strcat (pMensagem, ".");
    	}
    	
    	for(iLenCont=0; iLenCont<40; iLenCont++){
    		iContIndex = iLenCont + 10;
    		cDispPacote [iContIndex] = pMensagem [iLenCont];
    	}
    	
    	iBcc = cDispPacote [4];
    	cDispPacote [50] = iBcc;
    	cDispPacote [51] = '\0';
    	
    	for(iContIndex=0; iContIndex<=51; iContIndex++){
    		ToCom (iComDisplay, cDispPacote[iContIndex]);
    	}
    	
    	iDisplaySend=0;
    }

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Like this?
    Code:
    int count;
    char myCharArray[40];
    
    for(count = 0; count < 40; count++){
        myCharArray[count] = ' ';
    }
    Woop?

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Sorry, i had say array char, but its a pointer.

    unsigned char *pMensagem

    but i fill the cPacote, its a good idea i think...

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Doh. I should have spotted it, but you are actually appending to the parameter passed in - which often is a constant, so you should definitely not do that.

    How about this:
    Code:
    	
    	for(iLenCont=0; iLenCont<iLenMsg; iLenCont++){
    		iContIndex = iLenCont + 10;
    		cDispPacote [iContIndex] = pMensagem [iLenCont];
    	}
    	if (iLenMsg < 40){
    		for(iLenCont=iLenMsg; iLenCont<40; iLenCont++)
    		     cDispPacote [iContIndex] = '.';
    	}
    --
    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.

  5. #5
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Same concept assuming you have enough memory allocated
    Code:
    int counter;
    char *myCharPointer = malloc(40 * sizeof(char));
    
    for(counter = 0; counter < 40; counter++){
    	myCharPointer[counter] = ' ';
    }
    Woop?

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Code:
    if (iLenMsg < 40){
    		for(iLenCont=iLenMsg; iLenCont<40; iLenCont++)
    		     cDispPacote [iLenCont] = '.';
    	}
    matsp is king!

    prog-bman thanks.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    my display has a demon
    its not possible

    Code:
    for(iLenCont=0; iLenCont<iLenMsg; iLenCont++){
    		iContIndex = iLenCont + 10;
    		cDispPacote [iContIndex] = pMensagem [iLenCont];
    	}
    	if (iLenMsg < 40){
    		for(iLenCont=iLenMsg; iLenCont<50; iLenCont++)
    		     cDispPacote [iLenCont] = '.';
    	}
    all codes crash it.
    eheueheh

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    my display has a demon
    its not possible

    Code:
    for(iLenCont=0; iLenCont<iLenMsg; iLenCont++){
    		iContIndex = iLenCont + 10;
    		cDispPacote [iContIndex] = pMensagem [iLenCont];
    	}
    	if (iLenMsg < 40){
    		for(iLenCont=iLenMsg; iLenCont<50; iLenCont++)
    		     cDispPacote [iLenCont] = '.';
    	}
    all codes crash it.
    eheueheh
    No, I can't write code. Add change the second for-loop to:
    Code:
    	if (iLenMsg < 40){
    		for(iLenCont=iLenMsg; iLenCont<40; iLenCont++)
    		     iContIndex = iLenCont + 10;
    		     cDispPacote [iContIndex] = '.';
    	}
    --
    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.

  9. #9
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    thanks again
    tomorrow i will test this code
    now the equipment was out of here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from perl to C
    By prettydainty in forum C Programming
    Replies: 5
    Last Post: 01-05-2009, 11:47 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  4. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM