arguments question

Discussion in 'C Programming' started by parshin.da@gmail.com, Jan 17, 2008.

  1. Guest

    Hi, i have following problem:
    when i tried to run following code, i`ve got segmentation fault
    error
    char s[255];
    char d[1024];

    char* ps = s;
    char* pd = d;


    size_t ss = sizeof(s);
    size_t sd = ss;

    if( -1 == iconv( des, ( char**)&s, &ss, (char**)&pd, &d ) )
    ....

    and if i pass to iconv ps and pd instead of s and d, it`s all working
    good.What is the difference???

    sorry for my bad english:)
     
    , Jan 17, 2008
    #1
    1. Advertising

  2. Mark Bluemel Guest

    wrote:
    > Hi, i have following problem:
    > when i tried to run following code, i`ve got segmentation fault
    > error
    > char s[255];
    > char d[1024];
    >
    > char* ps = s;
    > char* pd = d;
    >
    >
    > size_t ss = sizeof(s);
    > size_t sd = ss;
    >
    > if( -1 == iconv( des, ( char**)&s, &ss, (char**)&pd, &d ) )


    &s is not the address at which you will find the address of a character.

    's' on its own is a pointer to char - a pointer to the first char in the
    array.

    '&s' is a pointer to array of char - a pointer to the whole array.
     
    Mark Bluemel, Jan 17, 2008
    #2
    1. Advertising

  3. Flash Gordon Guest

    wrote, On 17/01/08 14:58:
    > Hi, i have following problem:
    > when i tried to run following code, i`ve got segmentation fault
    > error


    What follows is *not* a complete compilable example. Next time please
    provide a complete compilable example since there may well be problems
    in what you are not showing us.

    > char s[255];
    > char d[1024];
    >
    > char* ps = s;
    > char* pd = d;
    >
    >
    > size_t ss = sizeof(s);
    > size_t sd = ss;


    For example, we do not know if des in the line below has been correctly
    set up.

    > if( -1 == iconv( des, ( char**)&s, &ss, (char**)&pd, &d ) )


    For those who do not know the prototype for iconv (at least the one I
    know about) is
    size_t iconv(iconv_t, char**, size_t*, char**, size_t*);

    > and if i pass to iconv ps and pd instead of s and d, it`s all working
    > good.What is the difference???


    Does "char s[255]" look line "char *ps"? What is the difference in
    types? What happens if you remove the casts you added?

    Note that adding casts because the compiler complains is almost always
    the WRONG thing to do. In this case it stopped the compiler from telling
    you that your code was wrong and did nothing to fix the problem.

    Pointers are not arrays and arrays are not pointer. Go to
    http://c-faq.com/ and read the comp.lang.c FAQ starting with section 6
    where the first 10 questions are dealing with your mis-conceptions.

    iconv is not actually topical in this group, although your problem is.
    So it would be worth checking in comp.unix.programmer if you have other
    questions about iconv rather than problems with the C language.

    > sorry for my bad english:)


    Bad English is not a problem when you have done your best.
    --
    Flash Gordon
     
    Flash Gordon, Jan 17, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Diener
    Replies:
    14
    Views:
    5,086
    Josiah Carlson
    Apr 6, 2004
  2. Neo
    Replies:
    10
    Views:
    692
    sushant
    Jan 20, 2005
  3. tutmann
    Replies:
    4
    Views:
    454
  4. jmborr
    Replies:
    1
    Views:
    449
    Stargaming
    Nov 3, 2007
  5. Replies:
    3
    Views:
    855
Loading...

Share This Page