IPv6 Address

This is a discussion on IPv6 Address within the Networking/Device Communication forums, part of the General Programming Boards category; How to get the IPv6 address in run time and print the same... I have did the following program, but ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    39

    IPv6 Address

    How to get the IPv6 address in run time and print the same... I have did the following program, but it is printing some garbage values. It is not taking the value which i am giving...

    Code:
    #include<stdio.h>
     struct in6_addr
     {
      unsigned char s6_addr[16];
     };
                                                                                                                                 
    int main()
    {
     int i;
     struct in6_addr ipv;
     ipv.s6_addr[16]="128.12.343.65.76.89.0.0.0.0.0.12.56.75.44.187.199";
     
     for(i=16;i>=1;i--)
                                                                                                                                 
       printf("&#37;u.",ipv.s6_addr[i]);
                                                                                                                                 
    }

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    This "128.12.343.65.76.89.0.0.0.0.0.12.56.75.44.187.199 " is a string (which is longer then 16 bytes)

    this is an array of 16 numbers:
    Code:
    {128,12,343,65,76,89,0,0,0,0,0,12,56,75,44,187,199}
    if you want to initialize your struct with such numbers you probably should do something like
    Code:
    struct in6_addr ipv ={{128,12,343,65,76,89,0,0,0,0,0,12,56,75,44,187,199}};
    and note that array indexes are from 0 to 15
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    and note that array indexes are from 0 to 15
    . . . in other words, your for loop, which loops from 16 to 1 inclusive, is not correct. [edit] It should go from 0 to 15 inclusive, because you want to look at element 0 first, and so on. [/edit]

    Also, &#37;u is for ints -- but %c actually handles unsigned chars. http://man.he.net/man3/printf
    Code:
    c      The  int argument is converted to an unsigned char,
                  and the resulting character is written.
    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.

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Quote Originally Posted by dwks View Post
    Also, %u is for ints -- but %c actually handles unsigned chars. http://man.he.net/man3/printf
    But when you print Ip you do not want to see something like '\n'.'\n'.nul.nul
    You want to see 10.10.0.0 so %c is out of scope here
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    D'oh! But shouldn't the argument be casted then? I'm not sure what would happen if you passed a one-byte number while printf was expecting a (say) four-byte one . . . .
    The value preserving approach calls for promoting those types to signed int, if that type can properly represent all the values of the original type, and otherwise for promoting those types to unsigned int. Thus, if the execution environment represents short as something smaller than int, unsigned short becomes int; otherwise it becomes unsigned int.

    The unsigned preserving rules greatly increase the number of situations where unsigned int confronts signed int to yield a questionably signed result, whereas the value preserving rules minimize such confrontations. Thus, the value preserving rules were considered to be safer for the novice, or unwary, programmer. After much discussion, the Committee decided in favor of value preserving rules, despite the fact that the UNIX C compilers had evolved in the direction of unsigned preserving.
    I'm assuming that applies to argument promotion as well.

    In short, this means that an unsigned char will be promoted to a signed int. Since &#37;u expects an unsigned int, I think that a cast to (unsigned) would be necessary. Or you could just use %i or %d. (They're the same thing.)
    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.

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    I do not think promoting unsigned char value to signed int and then printing it as &#37;u will cause any problems on any platform. I think problems could be with signed chars, not unsigned. But I my be wrong on this...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Actually, yes, you're probably right, because every value of an unsigned char can be represented with an int. So much for that idea.
    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.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,457
    It may be undefined behavior, but on x86, it works fine since any value pushed on the stack is 4 bytes. So your char becomes an int and can be read just fine from printf. The format specifier just tells it how to interpret the data.
    Plus the registers are 4 bytes, unless passed in a 1 or 2-byte register, which is unlikely, so even if passed via registers, it will probably work anyway. Plus I'd believe printf uses a __cdecl type of calling convention, passing all data on the stack and the caller cleaning up.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Quote Originally Posted by Elysia View Post
    It may be undefined behavior,
    No it is not. It is defined behavior stated in the standard

    If the expression that denotes the called function has a type that does not include a prototype, the integral promotions are performed on each argument and arguments that have type float are promoted to double.
    It has nothing to do with passing parameters on stack or in register
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 09:48 AM
  2. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  3. I thought pointers were pointers...
    By keira in forum C Programming
    Replies: 19
    Last Post: 08-15-2007, 11:48 PM
  4. DX - CreateDevice - D3DERR_INVALIDCALL
    By Tonto in forum Game Programming
    Replies: 3
    Last Post: 12-01-2006, 06:17 PM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM

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