Thread: Null String

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

    Null String

    Code:
    char *InBuf;	   			
    
    //.................
    
    int RecebeSerial(void){
    
        int iLenBuf, iReadReturn;
    		
    	if(IsCom(iComBalanca)){
    		iLenBuf = DataSizeInCom (iComBalanca);	
    		iReadReturn = ReadComn(iComBalanca, InBuf, iLenBuf);
    		
    		InBuf [iLenBuf] = '\0';
    		
    		if (iReadReturn >= 0){	
    			ProcessaBuffer (InBuf);
    		}
    		
    		return 1;
    	}
    	
    	return -1;
    }
    
    int strrpos (char *pStrText, char pChar){
    	
    	int iLen, iCont;
    	iLen = strlen (pStrText);
    
    	printCom1 (pStrText);
    	printCom1 ("\nSTR.: %s", pStrText);
    	printCom1 ("\nSTR1: %s", pStrText[1]);
    	printCom1 ("\nBUF.: %s", InBuf);
    	printCom1 ("\nLEN.: %d", iLen);
    	
    	if (iLen > 0){
    		for (iCont=iLen - 1; iCont>=0; iCont--){
    			if (pStrText[iCont] ==  pChar)
    				return iCont;
    		}
    		return 0;	// Não houve ocorrências
    	}
    	else { return 0; }	// String Nula
    }
    
    void ProcessaBuffer (char *pInBuf){
    
    	char cChar;
    	unsigned char *cPacote, *cPeso;
    	unsigned char *cStatus;
    	unsigned char cByte;
    	int iCont;
    	int iPosStart, iPosEnd, iPosTam; 
    
    	cChar = STARTCHAR; 
    	iPosStart = strrpos (pInBuf, cChar);
    	
    	//...

    strpos return:

    1234567890AB
    STR.: (null)
    STR1: ?
    BUF.: (null)
    LEN.: 0 STARTCHAR:  INT: 2 Posição: 0


    When i call strpos funcion, and use printCom1 (simillar printf) then result is ok, when I use %s or manipulate it string package is null.

    Sorry for my english.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So are you actually assigning inbuf with anything?

    --
    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.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Quote Originally Posted by matsp View Post
    So are you actually assigning inbuf with anything?

    --
    Mats
    Yes,

    With Received Buffer in Serial (RecebeSerial):

    iReadReturn = ReadComn(iComBalanca, InBuf, iLenBuf);

    And add a '\0'

    InBuf [iLenBuf] = '\0';

    After, I Call strpos function.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But ReadComm takes the inbuf as a parameter, not as a pointer, so ReadComm can not update the pointer.

    --
    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
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Code:
    int ReadComn(int port, unsigned char *buf, int n);
    Then, What I can make to use correctly ?

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by sergioms View Post
    Code:
    int ReadComn(int port, unsigned char *buf, int n);
    Then, What I can make to use correctly ?
    Either pass buf as unsigned char ** (and use &inbuf in the calling code), or use a different mechanism to assign the inbuf to some memory location.

    --
    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.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Ok, i have modified to unsigned char, and calling inbuf with &, but the result is:

    `1>ÿßâñhá
    STR.: `1>ÿßâñhá
    STR1: ?
    BUF.: `1>ÿßâñhá
    LEN.: 11STARTCHAR: INT: 2 Posição: 0
    ïHO
    STR.: ïHO
    STR1:
    BUF.: ïHO
    LEN.: 3 STARTCHAR: INT: 2 Posição: 0

    And the Compiler Warning: Suspicous pointer converter.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    There is no way for me to tell if that is correct or not.

    --
    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
    ok, ok sorry.

    [CODE]
    iReadReturn = ReadComn(iComBalanca, &InBuf, iLenBuf);
    ...
    ProcessaBuffer (&InBuf);

    ................

    int strrpos (unsigned char *pStrText, char pChar){ // Última ocorrência do caractere.

    int iLen, iCont;
    iLen = strlen (pStrText);

    printCom1 ("\nSTR.: %s", &pStrText);
    printCom1 ("\nLEN.: %d", iLen);

    .........

    void ProcessaBuffer (unsigned char *pInBuf){

    char cChar;
    unsigned char *cPacote, *cPeso;
    unsigned char *cStatus;
    unsigned char cByte;
    int iCont;
    int iPosStart, iPosEnd, iPosTam;

    cChar = STARTCHAR;
    iPosStart = strrpos (&pInBuf, cChar);
    ...
    [CODE]

    Send: 1234567890AB

    Result:

    STR.: 123
    LEN.: 14STARTCHAR:  INT: 2 Posição: 0

  10. #10
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    ok, ok sorry.

    [CODE]
    iReadReturn = ReadComn(iComBalanca, &InBuf, iLenBuf);
    ...
    ProcessaBuffer (&InBuf);

    ................

    int strrpos (unsigned char *pStrText, char pChar){ // Última ocorrência do caractere.

    int iLen, iCont;
    iLen = strlen (pStrText);

    printCom1 ("\nSTR.: %s", &pStrText);
    printCom1 ("\nLEN.: %d", iLen);

    .........

    void ProcessaBuffer (unsigned char *pInBuf){

    char cChar;
    unsigned char *cPacote, *cPeso;
    unsigned char *cStatus;
    unsigned char cByte;
    int iCont;
    int iPosStart, iPosEnd, iPosTam;

    cChar = STARTCHAR;
    iPosStart = strrpos (&pInBuf, cChar);
    ...
    [CODE]

    Send: 1234567890AB

    Result:

    STR.: 123
    LEN.: 14STARTCHAR:  INT: 2 Posição: 0

  11. #11
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    What's the code for ReadComn? Looks like you're getting confused with syntax. You're using &pInBuf, &InBuf and &pStrText in places where it is incorrect/inappropriate to do so and I suspect you're tripping over yourself.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  12. #12
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Code:
    int ReadComn(int port, unsigned char *buf, int n);
    then...How to Call this functions?

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, if you want to MODIFY what the parameter buf points to, you need to have a double pointer so that you can do something like *buf = newplace.

    --
    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.

  14. #14
    Registered User
    Join Date
    Nov 2008
    Location
    Santa Catarina - Brasil
    Posts
    184
    Oh god, sorry i don't undestand.

    Can you show me an example please?

  15. #15
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Not the function prototype, the actual code for the ReadComn function.

    If the parameter you're passing into the function needs to be changed then you need to pass an address (a pointer) to whatever you plan on changing. If what you plan on changing is already a pointer, then you need to pass in a pointer to a pointer and inside the function that will be making the change you'd modify it's value (such that the value is changed in the calling function) by doing like matsp stated above. This does not mean that you stick a & in front of every array/pointer variable in your code:

    Some examples of you missuse of &... Assuming InBuf is declared as an unsigned char*, then:
    Code:
    // This is fine as long as the pointer InBuf needs to be modified by the ReadComn function
    // and the function is defined to accept an unsigned char** and not an unsigned char*.
    // In the function, you'd change its value by doing *buf = something
    iReadReturn = ReadComn(iComBalanca, &InBuf, iLenBuf);
    ...
    // This is likely incorrect, the function is declared as taking an unsigned char* but &InBuf in this
    // case is a unsigned char** notice the extra *.  Does ProcessaBuffer also need to modify the
    // pointer's value?  If it does, then the function needs to accept an unsigned char** like ReadComn
    // If not, then you certainly don't want to use the &
    ProcessaBuffer (&InBuf);
    
    ................
    
    int strrpos (unsigned char *pStrText, char pChar){ // Última ocorrência do caractere.
    
    int iLen, iCont;
    iLen = strlen (pStrText);
    
    // This is incorrect, you use it correctly above with the strlen function (without the &)
    // but then you mess it up by putting the & back in here.
    printCom1 ("\nSTR.: %s", &pStrText);
    printCom1 ("\nLEN.: %d", iLen);
    
    .........
    
    void ProcessaBuffer (unsigned char *pInBuf){
    
    char cChar;
    unsigned char *cPacote, *cPeso;
    unsigned char *cStatus;
    unsigned char cByte;
    int iCont;
    int iPosStart, iPosEnd, iPosTam;
    
    cChar = STARTCHAR;
    // This here needs to change to just pInBuf.  pInBuf  is an unsigned char*.
    // &pInBuff would therefore be a unsigned char** but the strrpos function accepts an unsigned char*
    // You'd therefore need to use just pInBuf instead.
    iPosStart = strrpos (&pInBuf, cChar);
    ...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. addrinfo Structure Not Working
    By pobri19 in forum Networking/Device Communication
    Replies: 9
    Last Post: 10-22-2008, 10:07 AM
  2. . . . . . . - . . . - -
    By The Brain in forum C++ Programming
    Replies: 17
    Last Post: 05-17-2005, 04:01 AM
  3. Help with yacc/compiler design/seg fault
    By trippeer in forum C Programming
    Replies: 1
    Last Post: 04-08-2005, 03:43 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. File Reading and storing to 1 variable
    By Rare177 in forum C Programming
    Replies: 34
    Last Post: 07-13-2004, 12:58 PM