Thread: strncyp problem

  1. #1
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709

    strncpy problem

    Why the fook doesn't this work? I've never had any trouble with strncpy() before. Maybe I'm getting rusty.

    Code:
     
    void init_user_s (user_t* u, int uid, char* uname, char*pwd)
    { 
    	u->user_id = uid;
    	strncpy (u->username, uname, strlen(uname) + 1);
    	strncpy (u->password, pwd, strlen(pwd) + 1);
    }
    It compiles fine but I get a runtime error. Oh, here's the user_t struct for reference:

    Code:
    typedef struct 
    {
    	int user_id;
    	char* username;
    	char* password;
    } user_t;
    Last edited by cboard_member; 07-24-2005 at 10:51 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Your pointers in your structure don't point anywhere.
    I gues up to now, you've been lucky (that it worked) or unlucky (that it didn't crash the first time you ever made the mistake)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Ah very true. Oh and before you do your patented moaning attack, I realise that I spelt strncpy wrong in the title of the thread, and this is the first time I've written a function to initialise a structure, ergo this is the first time I've made this mistake (and it did crash).

    This is for after you've locked this thread or whatever:

    I knew you were going to do that.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  4. #4
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Updated code that still doesn't work:

    Code:
     
    /* init_user_s(): initialises a user structure with specified values */
    void init_user_s (user_t* u, int uid, char* uname, char* pwd)
    {
    	uname = (char*)calloc (sizeof(char), strlen(uname) + 1);
    	pwd = (char*)calloc (sizeof(char), strlen(pwd) + 1);
     
    	if (uid != 1) // user_id 1 reserved for administrator
    	{
    		printf ("Error, user ID 1 reserved.\n");
    		return;
    	}
     
    	u->user_id = uid;
    	strncpy (u->username, uname, strlen(uname) + 1);
    	strncpy (u->password, pwd, strlen(pwd) + 1);
    }
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    u->username
    Try allocating this

    uname is a parameter, which you trash with calloc

    Oh, and drop the cast as well - see the FAQ
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yeah about the casts, I can't seem to get it to compile if I type something like this:

    Code:
    char* buffer = malloc (sizeof(char) * 10);
    If it's easier just post up the FAQ link in question.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Stop compiling as C++. The FAQ isn't hard to find. As a matter of fact, it's right at the top of the page.


    Quzah
    Hope is the first step on the road to disappointment.

  8. #8
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Yes may be true. You seem to be quite good at being sarcastic quzah. I doubt it gets you anywhere in life so don't even bother replying to my threads in future. Can you do that? Appreciate it.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is true, you idiot. That's why I said it. If you can't compile without a cast, then stop compiling as C++. Furthermore, you're too lazy to click the FAQ link so you want it posted here? WTF? Why am I supposed to believe then that you'll actually read, or click a direct link, when you're too lazy to click FAQ and then scroll down?


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    I realise 'you idiot', that it is easy to get to and it's right there, but some of have a life. Ergo we are doing other things during the time that people like you are clicking links.

    WTF? I can't be bothered to read? What did I say above? It was along the lines of fhork off. Yeah, that's better. and don't answer my posts.
    Last edited by cboard_member; 07-25-2005 at 02:28 AM.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You need to include <stdlib.h> to make uncasted malloc() work.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 04:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM