characterss beyond null [ for C programmers]

  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 11:34 AM GMT
    Can somebody tell me how do I store a character beyond the null character in a string?

    Im using C, and something like

    char myvar[256] = {};

    then I fill in character by character trying to get my string to look like:

    H | E | L | L | O | null | M | O | R | E | null |

    but Im a getting a default segmentation error



    HELP!!!!!!!!!!!!!!!!!!!


    ps: this is a gay topic since im freaking out because I dont know how to put a damn char beyond null icon_evil.gif
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 11:39 AM GMT
    Never thought I'd get to use this twice
    4d650-post_2_1115665738.jpg

    If you learning C (are you really learning straight C?? I thought they didn't teach that anymore, I thought it was all C++ and other oop programming unless you were doing advanced high level stuff.. )

    anywho.. search google.. if you get bugger stuck I can speak to a mate who does this stuff.. *shudders* C *shudders* I'll stick with ma php/perl
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 11:44 AM GMT
    im doing network programming in Unix, so ya its C

    Im developing an IRC server(through sockets) and the messages will be passed that way from the client to the server and vice-versa. string | null | string | null. The server and the client have to unpack and prrrrrrrrrrrrrrrrrrrrint it out to stdout. I know enough C but I NEVA but NEVA tried to put a character beyond null.

    besides...do you think if google had something findable about this topic I would be asking here???? I just cant find anything !
  • UncleverName

    Posts: 741

    Apr 01, 2009 2:40 PM GMT
    Wow. What a weird place to post this.

    How are you filling the string? Straight for loop? Or sprintf or something like that?

    Do you get the same seg fault error if you do this:

    #include
    #include
    .
    .
    memset( myvar, 0, sizeof( myvar ) );

    That should fill the entire array with nulls. If that doesn't cause a seg fault, then something else weird is going on.

    Writing past the null character should not matter at all. In C, strings are not strings, they are just memory buffers that happen to have characters in them. By convention, they end with null (0), but this doesn't mean anything in terms of memory or seg faults.

    A seg fault is usually caused by writing to memory that hasn't been allocated.
    So in your example, doing this should cause a seg fault:
    myvar[sizeof(myvar) + 1] = 0;
    My guess is that you are writing to memory past the 256th entry.

    (Note that sizeof only works on character arrays declared on the stack, not allocated with new)

    How are you finishing off your list of strings? 2 null characters? Are you fitting the whole thing in 256 characters? The example string you gave (Hello|0|More|0) probably needs to be double null terminated, so if it doesn't (meaning you have a fixed number of strings in your message), the total number of characters is 11. If you double null terminate the whole message, then you'd have 12 characters.

    Another quick sanity check would be to up the size of the array by something huge. Bump it to 2048 and see if you still have the seg fault.

    Hope that helps.
    Feel free to pm me if you're still stuck.
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 3:06 PM GMT
    thanks michael, I actually managed to set it up but what if I want to take it back ? for instance what if i add two strings so that one of them will hold the whole list sperated by a null character and then I want other variable to take the second string after the null.

    here is my code



    // this function works great it adds the second string after the null properly
    int build_stream(char * base , char * add)
    {
    int i;
    int len = (strlen(base) + strlen(add)) + 1;

    for (i = strlen(base) + 1; i < len ; i++)
    {
    base[i] = *add++;
    }

    base[len] = '';

    return len;
    }

    //here is what im trying to do to get the string back
    void getit(char * dest, char * text)
    {
    int i, j;
    int limit = 3;
    for (i = limit; *text != ''; i++)
    {
    dest[i-limit] = text[i];
    text++;
    }
    dest[i - limit] = '';
    }

    int main()
    {
    char myvar[256] = "OK";
    char * t = "HELLO";

    char user[15] = {}; //this variable will take 'HELLO' back

    build_stream(myvar, t); //after this call myvar holds OKnullHELLOnull
    getit(user, myvar); // here user never gets HELLO back

    //this is just for testing
    printf("myvar: %sn", myvar);
    printf("user : %sn", user);
    printf("t : %sn", t);

    }


    when I output user it gives me 'HL'

    its doing something its just jumping way too much...
  • swimbikerun

    Posts: 2835

    Apr 01, 2009 3:13 PM GMT
    Are you building this from the ground up Charlitos? And dude, why C? CSharp wouldn't be easier?
  • UncleverName

    Posts: 741

    Apr 01, 2009 3:16 PM GMT
    So here's a function that (assuming it compiles) should do what you want.

    It isn't very good, as it assumes that you're trying to get the second string out of 2 null terminated strings stored in memory sequentially. That's bad, because the actual network message passing you're doing is probably going to be a bit different from that (i.e. you're probably going to want to pass multiple strings). Course, I'm not exactly sure what you want to do past this one programming problem. Anyways, here goes:

    void getit(char * dest, char * text)
    {
    // skip past the first string and it's null character
    dest += strlen( dest ) + 1;

    // copy the second string into the first
    strcpy( text, dest );
    }

    The way I modified the function, it makes all kind of assumptions. It assumes that there are in fact 2 strings stored one after the other, null terminated. It assumes that the second string will fit in the first one, which it may or may not.
    In terms of what you actually posted in your main though, that function should work.
  • UncleverName

    Posts: 741

    Apr 01, 2009 3:20 PM GMT
    swimbikerun saidAre you building this from the ground up Charlitos? And dude, why C? CSharp wouldn't be easier?


    D'uh! He said that it's for a IRC program running on Unix.
    Unless you want to go through the hassle of figuring out how to install and use Mono on Unix, C# is out.

    Unless of course he just used ASP.net or whatever the blue tech is that basically does what Flash does for MS.

    Of course, learning how to do something super simple like this in C will teach him tons more about programming than doing something like this in C#. String manipulation is pretty basic, and fairly easy to not understand if you always use languages that treat strings as first class citizens (i.e. C#, Python, Perl, PHP, ActionScript, Ruby, etc).

  • swimbikerun

    Posts: 2835

    Apr 01, 2009 3:28 PM GMT
    UncleverName said
    swimbikerun saidAre you building this from the ground up Charlitos? And dude, why C? CSharp wouldn't be easier?


    D'uh! He said that it's for a IRC program running on Unix.
    Unless you want to go through the hassle of figuring out how to install and use Mono on Unix, C# is out.

    Unless of course he just used ASP.net or whatever the blue tech is that basically does what Flash does for MS.

    Of course, learning how to do something super simple like this in C will teach him tons more about programming than doing something like this in C#. String manipulation is pretty basic, and fairly easy to not understand if you always use languages that treat strings as first class citizens (i.e. C#, Python, Perl, PHP, ActionScript, Ruby, etc).

    Oh missed the Unix part. Unix and not a Linux flavor? omg
    Ok then wouldn't JAVA be acceptable? It's my personal preference but there's no need to reinvent the wheel right?
    Seems like a daunting task to build an entire IRC for Unix using C. Why not just implement something like Jabber and be done with it? Oh well...
    http://www.jabber.com/
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 3:41 PM GMT
    man.. all I gotta say is.. I love you lol

    it works

    i just had to swap the variables and it worked just great.

    I know about all the issues when my server receives all these messages , Im looping the send() call till it sends all the info same for recv() and since the buffers have a static size, it should work fine, I know wastes resources but in the mean time, I want to have something working and then optimize.

    About C#...Ive been programming in C# before, it is easy and pretty, yet not portable and M$'s...I also know C++ and Java...which are very good as well...but when it comes to Unix programming you need to learn C, Unix was built on C...and seriously right now...Java and C# seem like a child's play to me, once you learn C or Assembly you actually know what computer programming stands for icon_wink.gif

  • swimbikerun

    Posts: 2835

    Apr 01, 2009 3:47 PM GMT
    You guys are totally outgeeking me! hawt
    Yeah I took a class in Assembly a looong time ago. I think only three people in the whole class finished all the assignments correctly!
    Yeah CSharp is pretty easy, still trying to get my head around Java fully...
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 3:55 PM GMT
    swimbikerun saidYou guys are totally outgeeking me! hawt
    Yeah I took a class in Assembly a looong time ago. I think only three people in the whole class finished all the assignments correctly!
    Yeah CSharp is pretty easy, still trying to get my head around Java fully...


    man it all depends on what kinda of developer you are. Web Developer, Application Developer, it might be enough with Java || JSP and C# || .Net but what if you are working in a project where you have to create a compiler or make modifications to the operating system, I dont think Java will be any useful...
  • UncleverName

    Posts: 741

    Apr 01, 2009 4:04 PM GMT
    Just guessing here, but you probably don't have to do an entire IRC client, right Charlitos? You're probably making a pretty simple executable that can send and receive messages. So you basically have to learn how to use sockets to send and receive, and how to package up your messages. If I'm right, it's not too complicated.

    Jabber is pretty cool. We're using that for our chats at my current work. It's safe, secure and private as well, which msn/yahoo isn't really.

    I would assume that you could actually code C++ on Unix. You definitely can on Linux. If you're using gcc to compile, you can probably just use g++ to compile instead. Although if you're at university, the computer's might be so old that they don't have it.

    And you'd be surprised at what you should be able to do with Java. You probably can't, but that's mainly because they tried to make it cross platform, which means doing OS specific stuff is pretty hard.

    I'm a big C# fan. It's like the best of both Java and C++. Relatively easy to learn, and the dev environment (Visual Studio) is the best IDE on the planet. I just wish Visual Studio and C# worked on Mac and Linux.

    Ok, geek rant over.
  • swimbikerun

    Posts: 2835

    Apr 01, 2009 4:14 PM GMT
    charlitos said
    swimbikerun saidYou guys are totally outgeeking me! hawt
    Yeah I took a class in Assembly a looong time ago. I think only three people in the whole class finished all the assignments correctly!
    Yeah CSharp is pretty easy, still trying to get my head around Java fully...


    man it all depends on what kinda of developer you are. Web Developer, Application Developer, it might be enough with Java || JSP and C# || .Net but what if you are working in a project where you have to create a compiler or make modifications to the operating system, I dont think Java will be any useful...
    1110101011000 [<-c0mput3r j0k3]beep beep bloop

    Oh and you guys are BOTH characters way, way beyond NULL! omg

    Oh now I see what's going on... This is a Canadian Hot Nerd Conspiracy!!!
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 5:26 PM GMT
    UncleverName saidJust guessing here, but you probably don't have to do an entire IRC client, right Charlitos? You're probably making a pretty simple executable that can send and receive messages. So you basically have to learn how to use sockets to send and receive, and how to package up your messages. If I'm right, it's not too complicated.

    Jabber is pretty cool. We're using that for our chats at my current work. It's safe, secure and private as well, which msn/yahoo isn't really.

    I would assume that you could actually code C++ on Unix. You definitely can on Linux. If you're using gcc to compile, you can probably just use g++ to compile instead. Although if you're at university, the computer's might be so old that they don't have it.

    And you'd be surprised at what you should be able to do with Java. You probably can't, but that's mainly because they tried to make it cross platform, which means doing OS specific stuff is pretty hard.

    I'm a big C# fan. It's like the best of both Java and C++. Relatively easy to learn, and the dev environment (Visual Studio) is the best IDE on the planet. I just wish Visual Studio and C# worked on Mac and Linux.

    Ok, geek rant over.


    Uhmm, I dont know what you mean by an entirely IRC client...I also have to build the server. Basically it has to support a bunch of users limit 100 sending messages to each other. They can also send messages to a group of users and the server just keeps logs of all the connection and commands sent from the user. Not sure how complete this would be. Im building my server so it supports ipv4 and ipv6.

    We were taught Java and Python here at UofT during the first 2 years. So now they are like getting more into purpose of how to deal with the memory and stuff. I Believe most of the unix stuff are built on C, so it is really important to understand C and be able to modify code. Thats the reason why we are struggling with C right now.

    I love C#, it looks pretty much like Java just a bit "smarter". It is almost perfectly structured and really easy to learn indeed.

    Anyway every language has something that makes it the best one on specific tasks. As a programmer we just need to know who is better for what under a set of circunstances. Sometimes will be C, sometimes will be Java, sometimes will be Perl or C#.

  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 5:31 PM GMT
    swimbikerun said

    Oh and you guys are BOTH characters way, way beyond NULL! omg

    Oh now I see what's going on... This is a Canadian Hot Nerd Conspiracy!!!


    the truth is...you cant 11011 us lol icon_cool.gif
  • UncleverName

    Posts: 741

    Apr 01, 2009 7:36 PM GMT
    charlitos saidUhmm, I dont know what you mean by an entirely IRC client...I also have to build the server. Basically it has to support a bunch of users limit 100 sending messages to each other. They can also send messages to a group of users and the server just keeps logs of all the connection and commands sent from the user. Not sure how complete this would be. Im building my server so it supports ipv4 and ipv6.


    I basically figured that you don't have to do a huge user interface for the chat program, with the ability to search history and have animated smilies and such. Maybe I'm wrong though. Sounds like you're doing a bit more than I figured.

    charlitos saidAnyway every language has something that makes it the best one on specific tasks. As a programmer we just need to know who is better for what under a set of circunstances. Sometimes will be C, sometimes will be Java, sometimes will be Perl or C#.


    No, Cobol is the best language ever. For everything. You're just flat out wrong.

    Kidding, of course.
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 7:40 PM GMT
    its an IRC for a unix terminal, no interface, no smily faces lol. Just communication icon_razz.gif


    youre right about Cobol, I apologize!!!! icon_redface.gif
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 7:43 PM GMT
    icon_eek.gifI'm so freakin' HOT right now!icon_eek.gif
  • MattyC0709

    Posts: 1199

    Apr 01, 2009 7:51 PM GMT
    I thought you can't really put in null characters wherever you want, just at the end of you character string. Doesn't putting a '/0' mean you cannot read further than that '/0'? icon_confused.gif
  • Posted by a hidden member.
    Log in to view his profile

    Apr 01, 2009 8:00 PM GMT
    muttskins saidI thought you can't really put in null characters wherever you want, just at the end of you character string. Doesn't putting a '/0' mean you cannot read further than that '/0'? icon_confused.gif


    thats a convention not a rule. A null character is just another character that string functions in C use to know where the string finishes otherwise they end up accessing illegal memory addresses. Once again that doesnt mean you cant put nulls whenever you want. You do with the memory space whatever you want and as long as you know how to work with pointers you should be fine.

    however pointers are not supported on Java or C# nor many other Programming Language, they require really strong programming skills and lead to a bunch of illegal memory access and segmentation fault as in my example if you dont use them properly. However classes in C# and Java are references, the same for strings and array declarations as far as im concerned, since these also instantiate from a class.
  • Posted by a hidden member.
    Log in to view his profile

    Apr 02, 2009 11:59 AM GMT
    UncleverName saidSo here's a function that (assuming it compiles) should do what you want.

    It isn't very good, as it assumes that you're trying to get the second string out of 2 null terminated strings stored in memory sequentially. That's bad, because the actual network message passing you're doing is probably going to be a bit different from that (i.e. you're probably going to want to pass multiple strings). Course, I'm not exactly sure what you want to do past this one programming problem. Anyways, here goes:

    void getit(char * dest, char * text)
    {
    // skip past the first string and it's null character
    dest += strlen( dest ) + 1;

    // copy the second string into the first
    strcpy( text, dest );
    }

    The way I modified the function, it makes all kind of assumptions. It assumes that there are in fact 2 strings stored one after the other, null terminated. It assumes that the second string will fit in the first one, which it may or may not.
    In terms of what you actually posted in your main though, that function should work.


    Hm... it's been years since I last coded in C++ for SCHOOL! hehe. So anyway, I seem to recall the strcat function. Can't it be used instead of doing multiple strcpy's after the first one?

    Your objective is to output a concatenated string from separate inputs of null terminated strings, correct?
  • bradsmith

    Posts: 175

    Apr 02, 2009 12:26 PM GMT
    I am, finally, impressed...and flabbergasted at an intelligent post on RJ that didn't degenerate...even with the mention of COBOL!
  • Posted by a hidden member.
    Log in to view his profile

    Apr 02, 2009 12:46 PM GMT
    Sedative said

    Your objective is to output a concatenated string from separate inputs of null terminated strings, correct?


    no, I cant use strcat. strcat() will concatenate the second string right after the first one, meaning it will write on top of the null character in my first string, which makes total sense.

    I already made the function, now it works great, I can concatenate as many strings as I want separated by a null character. Here it is:

    int build_stream(const char * add, size_t offset, char * base ,  int size)
    {
    //make sure I dont try to write beyond the buffer's length
    if ( (strlen(add) + offset) > size)
    return -1;

    // adding the string 'add' to 'base' after null
    strcpy(base + offset + 1, add);

    // return size of the new string created
    return offset + 1 + strlen(add);
    }



    this function will also return the size of the new created string since strlen() won't get whats after the first null, I have to find a way to keep track of the real size of this variable.

    here it is how I would call this function



    int main()
    {
    char myvar[256] = "OK";

    char *add1 = "HI";
    char *add2 = "THERE";

    size_t , pos;

    pos = strlen(myvar);

    pos = build_stream(add1, pos, myvar, sizeof myvar);
    pos = build_stream(add2, pos, myvar, sizeof myvar);

    /* 'myvar' now contains "OKnullHInullTHEREnull" */
    return 0;
    }