Weird struct pointer return

This is a discussion on Weird struct pointer return within the C Programming forums, part of the General Programming Boards category; Code: void function(struct LoginInfo *login_info) { strcpy(login_info->RouterAddress, "1.0.0.1"); strcpy(login_info->Password, "hello world"); } main() { struct LoginInfo { char RouterAddress[30], Login[30], ...

  1. #1
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768

    Weird struct pointer return

    Code:
    void function(struct LoginInfo *login_info)
    {   
       strcpy(login_info->RouterAddress, "1.0.0.1");
       strcpy(login_info->Password, "hello world");
    }
    
    main()
    {
       struct LoginInfo {
          char RouterAddress[30], Login[30], Password[30];
       } login_info;
    
       strcpy(login_info.Login, "root");
       function(&login_info);
    }
    The weird thing that the returned Login is not the one I sent to the function ("root")... it's some just some value, is that normal???


    Thank you.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    Code:
    gcc -W -Wall -ansi -pedantic -O2 hello.c -lefence
    hello.c:5: warning: `struct LoginInfo' declared inside parameter list
    hello.c:5: warning: its scope is only this definition or declaration, which is probably not what you want
    hello.c: In function `function':
    hello.c:7: warning: implicit declaration of function `strcpy'
    hello.c:7: dereferencing pointer to incomplete type
    hello.c:8: dereferencing pointer to incomplete type
    hello.c: At top level:
    hello.c:12: warning: return type defaults to `int'
    hello.c: In function `main':
    hello.c:18: warning: passing arg 1 of `function' from incompatible pointer type
    Make your struct visible to both things - at the moment, it's local to main, and the struct in the function prototype is actually a different struct.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    yeah i see your point, but it's not the original code...
    in the real code the struct is known by the function.

    anyway, I've found the problem, I had a small overflow in the RouterAddress, so the Login got messed up. Sorry for this really stupid thread.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 06:03 AM
  2. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 09:36 PM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  4. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 02:39 PM
  5. C++ FTP class won't work
    By lord mazdak in forum C++ Programming
    Replies: 8
    Last Post: 12-18-2005, 07:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21