The new forums will be named Coin Return (based on the most recent vote)! You can check on the status and timeline of the transition to the new forums here.
The Guiding Principles and New Rules document is now in effect.

Stupid C strings..... [fixed]

Sharp101Sharp101 TorontoRegistered User regular
edited November 2006 in Help / Advice Forum
ok, this should be a quick question....


writing a Sender/Reciever program

Both files send string "ACK" to eachother, both defined in each file the same:
char ack[3]="ACK"; //defined ACK


sooo now....


before I send or recieve anything, I print out what the variable is
both have this line
printf("expected ack: %s \n",ack);

now lets see what the two files actually print.

Sender prints
expected ack: ACÿÏ1

Reciever prints
expected ack: ACKÿ


So when I send this to the other file, it doesn't compare properly.


Ideas?

(summary.... the same string declared the same way in two different files prints differently. Why?)

Sharp101 on

Posts

  • MiracleManSMiracleManS __BANNED USERS regular
    edited November 2006
    Are you sure that you're not adding extra characters to the end by accident and/or there isn't a problem with this in one of the settings of the variable you're doing?

    I know in PHP now there's an add-on to the timestamp string if you're not careful.

    MiracleManS on
    goldfishsig.jpg
  • taerictaeric Registered User, ClubPA regular
    edited November 2006
    Is there a reason you aren't null terminating those? I thought a string in C was typically a null terminated character array.

    so, shouldn't it be?
    char ack[4] = "ACK\0";
    

    taeric on
  • Sharp101Sharp101 TorontoRegistered User regular
    edited November 2006
    Thanks Taeric, that got it....


    now onto the segmentation faults!

    Sharp101 on
  • taerictaeric Registered User, ClubPA regular
    edited November 2006
    Just a quick pointer, odds are that not null terminating the strings were causing the segmentation faults.

    taeric on
  • Sharp101Sharp101 TorontoRegistered User regular
    edited November 2006
    nope, opening a file that doesnt exist and trying to read from it does.

    :D

    Sharp101 on
  • homeobockshomeobocks Registered User regular
    edited November 2006
    When I saw the subject for this thread, I thought He forgot to null-termitate the string. Lo and behold.

    homeobocks on
  • sirSolariussirSolarius Registered User regular
    edited November 2006
    taeric wrote:
    Is there a reason you aren't null terminating those? I thought a string in C was typically a null terminated character array.

    so, shouldn't it be?
    char ack[4] = "ACK\0";
    

    When you quote a string, it includes the null terminator. Now your string is:

    ack[0]='A'
    ack[1]='C'
    ack[2]='K'
    ack[3]='\0' (the one you put in)
    ack[4]='\0' (the one that the string automatically puts in)

    ack[4] is not ack's allocated memory, so you're shoving a 0 somewhere that a 0 might not want to be.

    Also, you're pointing ack to "ACK" in your data/strings section, rather than copying the letters A, C, K and \0 into ack.

    The correct version is:
    char ack[4];
    strcpy(ack, "ACK");
    

    or
    char *ack = "ACK";
    

    sirSolarius on
This discussion has been closed.