strccpy

Discussion in 'C Programming' started by proxy foxy, Jun 3, 2010.

  1. proxy foxy

    proxy foxy Guest

    Hi,

    I understand from the solaris man page that strccpy compress the c
    escape sequence from the source and copy the string to target.
    I wrote the following program to realize the same. But the output
    seams to be same. Please let me know in what way can I realize the
    output:

    $> gcc temp.c -o temp -lgen

    int main ()
    {
    const char *s1="Hello\nWorld";
    char *s2 = (char *)malloc (30);
    printf ("s1[%d] : %s\n", strlen(s1), s1);
    s2 = strccpy (s2, s1);

    printf ("s2[%d] : %s\n", strlen(s2), s2);

    }

    I know this isn't a standard C function. Thought of seeking help
    however
    --
    Dust
     
    proxy foxy, Jun 3, 2010
    #1
    1. Advertising

  2. proxy foxy

    Gene Guest

    On Jun 3, 12:45 am, proxy foxy <> wrote:
    > Hi,
    >
    > I understand from the solaris man page that strccpy compress the c
    > escape sequence from the source and copy the string to target.
    > I wrote the following program to realize the same. But the output
    > seams to be same. Please let me know in what way can I realize the
    > output:
    >
    > $> gcc temp.c -o temp -lgen
    >
    > int main ()
    > {
    >         const char *s1="Hello\nWorld";
    >         char *s2 = (char *)malloc (30);
    >         printf ("s1[%d] : %s\n", strlen(s1), s1);
    >         s2 = strccpy (s2, s1);
    >
    >         printf ("s2[%d] : %s\n", strlen(s2), s2);
    >
    > }
    >
    > I know this isn't a standard C function. Thought of seeking help
    > however
    > --
    > Dust


    No. The compiler honors the escape sequences during scanning and
    parsing of a C string literal. By the time strcpy() runs, the escapes
    are long gone.

    The 's2 =' in

    s2 = strccpy (s2, s1);

    is redundant but not erroneous.


    The (char *) in the line with malloc() is redundant.

    You could replace the malloc() and strcpy() with strdup(). But this
    isn't strictly required.
     
    Gene, Jun 3, 2010
    #2
    1. Advertising

  3. proxy foxy

    proxy foxy Guest

    On Jun 3, 11:01 am, Gene <> wrote:
    > On Jun 3, 12:45 am, proxy foxy <> wrote:
    >
    >
    >
    > > Hi,

    >
    > > I understand from the solaris man page that strccpy compress the c
    > > escape sequence from the source and copy the string to target.
    > > I wrote the following program to realize the same. But the output
    > > seams to be same. Please let me know in what way can I realize the
    > > output:

    >
    > > $> gcc temp.c -o temp -lgen

    >
    > > int main ()
    > > {
    > >         const char *s1="Hello\nWorld";
    > >         char *s2 = (char *)malloc (30);
    > >         printf ("s1[%d] : %s\n", strlen(s1), s1);
    > >         s2 = strccpy (s2, s1);

    >
    > >         printf ("s2[%d] : %s\n", strlen(s2), s2);

    >
    > > }

    >
    > > I know this isn't a standard C function. Thought of seeking help
    > > however
    > > --
    > > Dust

    >
    > No.  The compiler honors the escape sequences during scanning and
    > parsing of a C string literal. By  the time strcpy() runs, the escapes
    > are long gone.
    >
    > The 's2 =' in
    >
    >        s2 = strccpy (s2, s1);
    >
    > is redundant but not erroneous.
    >
    > The (char *) in the line with malloc() is redundant.
    >
    > You could replace the malloc() and strcpy() with strdup().  But this
    > isn't strictly required.


    This is strccpy and not strcpy. For 'char *', I have non-ANSI
    compiler. For 's2 =', I agree with you.
     
    proxy foxy, Jun 3, 2010
    #3
  4. On 3 June, 07:01, Gene <> wrote:
    > On Jun 3, 12:45 am, proxy foxy <> wrote:


    > > I understand from the solaris man page that strccpy compress the c
    > > escape sequence from the source and copy the string to target.
    > > I wrote the following program to realize the same. But the output
    > > seams to be same. Please let me know in what way can I realize the
    > > output:

    >
    > > $> gcc temp.c -o temp -lgen

    >
    > > int main ()
    > > {
    > >         const char *s1="Hello\nWorld";
    > >         char *s2 = (char *)malloc (30);
    > >         printf ("s1[%d] : %s\n", strlen(s1), s1);
    > >         s2 = strccpy (s2, s1);

    >
    > >         printf ("s2[%d] : %s\n", strlen(s2), s2);
    > > }

    >
    > > I know this isn't a standard C function. Thought of seeking help
    > > however

    >
    > No.  The compiler honors the escape sequences during scanning and
    > parsing of a C string literal. By  the time strcpy() runs, the escapes
    > are long gone.
    >
    > The 's2 =' in
    >
    >        s2 = strccpy (s2, s1);
    >
    > is redundant but not erroneous.
    >
    > The (char *) in the line with malloc() is redundant.
    >
    > You could replace the malloc() and strcpy() with strdup().  But this
    > isn't strictly required.


    and strdup() is non-standard
     
    Nick Keighley, Jun 3, 2010
    #4
  5. proxy foxy <> wrote:
    > I understand from the solaris man page that strccpy compress the c
    > escape sequence from the source and copy the string to target.
    > I wrote the following program to realize the same. But the output
    > seams to be same. Please let me know in what way can I realize the
    > output:


    > $> gcc temp.c -o temp -lgen


    > int main ()
    > {
    > const char *s1="Hello\nWorld";
    > char *s2 = (char *)malloc (30);
    > printf ("s1[%d] : %s\n", strlen(s1), s1);
    > s2 = strccpy (s2, s1);


    > printf ("s2[%d] : %s\n", strlen(s2), s2);


    > }


    > I know this isn't a standard C function. Thought of seeking help
    > however


    I can't say for sure (since it's a non-standard function and
    I don't have any strccpy() function) but it looks to me as if
    your example program isn't a good use case of that function.
    If you would start out with

    const char *s1="Hello\\nWorld";

    (note the double backslash) then the string will contain, after
    the compiler is done with it, a backslash character directly
    followed by a 'n'. And I guess that this strccpy() function is
    meant to convert character sequences like this. In this case it
    replaces the backslash and the 'n' by the single character '\n'.
    So this function might be useful in cases where you have input
    consisting of C code (or some other language that uses the same
    escape character convention) and you want to extract the strings
    there-in and convert them to strings like the compiler would do
    it. Might be nice to have when you write an interpretor or com-
    piler.
    Regards, Jens
    --
    \ Jens Thoms Toerring ___
    \__________________________ http://toerring.de
     
    Jens Thoms Toerring, Jun 3, 2010
    #5
  6. proxy foxy

    Curtis Dyer Guest

    proxy foxy <> wrote:

    > Hi,
    >
    > I understand from the solaris man page that strccpy compress
    > the c escape sequence from the source and copy the string to
    > target. I wrote the following program to realize the same. But
    > the output seams to be same. Please let me know in what way can
    > I realize the output:


    As you mention, it's not a Standard C function, but from what I can
    tell, it seems you have reversed strccpy()'s concept (see below).

    > $> gcc temp.c -o temp -lgen


    Do you have the necessary #include's in your actual code? In the
    code you post here, you're missing <stdio.h>, <string.h>, and
    <stdlib.h>. I imagine there's a header file for strccpy()'s
    prototype as well.

    > int main ()
    > {
    > const char *s1="Hello\nWorld";


    Try changing this to:

    const char *s1 = "Hello\\nWorld";

    I think you'll see the slip in your reasoning.

    > char *s2 = (char *)malloc (30);


    You should check the return value of malloc(). Personally, I
    generally like to do so even in most test programs.

    Also, the cast here is unnecessary. If you've indeed forgotten to
    include <stdlib.h>, the cast would likely mask a diagnostic that
    would inform you of the fact.

    > printf ("s1[%d] : %s\n", strlen(s1), s1);


    I don't think it's an issue in this program, but it seems pretty
    typical to use `%lu' for size_t values. In C99, I believe `%zu'
    would be the appropriate conversion specifier for representing
    size_t objects' values.

    printf("s1[%lu] : %s\n", (unsigned long)strlen(s1), s1);

    <snip>

    > I know this isn't a standard C function. Thought of seeking
    > help however


    You might try checking the mailing lists of the vendors of
    particular libraries. In this case, there may be a newsgroup that
    covers Solaris programming.

    --
    "Don't worry about efficiency until you've attained correctness."
    - Eric Sosman, c.l.c
     
    Curtis Dyer, Jun 3, 2010
    #6
  7. proxy foxy

    proxy foxy Guest

    On Jun 3, 2:22 pm, Curtis Dyer <> wrote:
    > proxy foxy <> wrote:
    > > Hi,

    >
    > > I understand from the solaris man page that strccpy compress
    > > the c escape sequence from the source and copy the string to
    > > target.  I wrote the following program to realize the same. But
    > > the output seams to be same. Please let me know in what way can
    > > I realize the output:

    >
    > As you mention, it's not a Standard C function, but from what I can
    > tell, it seems you have reversed strccpy()'s concept (see below).
    >
    > > $> gcc temp.c -o temp -lgen

    >
    > Do you have the necessary #include's in your actual code?  In the
    > code you post here, you're missing <stdio.h>, <string.h>, and
    > <stdlib.h>. I imagine there's a header file for strccpy()'s
    > prototype as well.
    >
    > > int main ()
    > > {
    > >         const char *s1="Hello\nWorld";

    >
    > Try changing this to:
    >
    >   const char *s1 = "Hello\\nWorld";
    >
    > I think you'll see the slip in your reasoning.
    >
    > >         char *s2 = (char *)malloc (30);

    >
    > You should check the return value of malloc().  Personally, I
    > generally like to do so even in most test programs.
    >
    > Also, the cast here is unnecessary.  If you've indeed forgotten to
    > include <stdlib.h>, the cast would likely mask a diagnostic that
    > would inform you of the fact.
    >
    > >         printf ("s1[%d] : %s\n", strlen(s1), s1);

    >
    > I don't think it's an issue in this program, but it seems pretty
    > typical to use `%lu' for size_t values.  In C99, I believe `%zu'
    > would be the appropriate conversion specifier for representing
    > size_t objects' values.
    >
    >   printf("s1[%lu] : %s\n", (unsigned long)strlen(s1), s1);
    >
    > <snip>
    >
    > > I know this isn't a standard C function. Thought of seeking
    > > help however

    >
    > You might try checking the mailing lists of the vendors of
    > particular libraries.  In this case, there may be a newsgroup that
    > covers Solaris programming.
    >
    > --
    > "Don't worry about efficiency until you've attained correctness."
    >   - Eric Sosman, c.l.c



    Thanks to you all. I got the same reply from comp.unix.solaris
    http://groups.google.com/group/comp.unix.solaris/browse_thread/thread/8f3a71ef29706e51#

    --
    Dust
     
    proxy foxy, Jun 3, 2010
    #7
  8. proxy foxy <> writes:

    > On Jun 3, 11:01 am, Gene <> wrote:
    >> On Jun 3, 12:45 am, proxy foxy <> wrote:

    <snip>
    >> >         char *s2 = (char *)malloc (30);

    <snip>
    >> The (char *) in the line with malloc() is redundant.

    <snip>
    > For 'char *', I have non-ANSI compiler.


    So what does malloc return, then, in this non-ANSI implementation? All
    the non-ANSI systems I've used have malloc return a char * so the cast
    would be redundant in those cases as well.

    I suspect you have not fully understood what is happening will malloc
    and its return value/type. The program you showed would only be correct
    if malloc were defined to return an int and that is most unlikely.

    The reason this matters is that getting function return types and/or
    prototypes wrong is important -- more important than the \\
    misunderstanding that caused you to post.

    --
    Ben.
     
    Ben Bacarisse, Jun 3, 2010
    #8
    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.

Share This Page