functions declaration, pointers, cast, .... CONFUSING

This is a discussion on functions declaration, pointers, cast, .... CONFUSING within the C Programming forums, part of the General Programming Boards category; Hi all, I'm in the most confusing period of learning the C language. What's worse is that my book keeps ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    Question functions declaration, pointers, cast, .... CONFUSING

    Hi all,

    I'm in the most confusing period of learning the C language. What's worse is that my book keeps writing exercises using commands and method that have not been taught yet! Anyhow, could someone please try to explain WHY and WHEN a function would be declared like the following:

    char * compare ( char * , char *);

    I don't understand the asterisk before the function name. This is the function header:

    char * compare ( chat *str1, char *str2 )

    and it returns the following

    if ( x > y )
    return ( str1);
    else if ( x < y )
    return (str2);

    As long as I didn't have the asterisk before the function name, I got tonnes of errors and warnings.
    could someone please explain?
    Rhodium
    Thanx
    Rhodium

  2. #2
    personification
    Guest
    Strings are dealt with in C as pointers. Look at your function like this:

    Code:
    char* compare ( chat *str1, char *str2 )
    You're returning a char pointer. If you used char by itself you would be returning a single byte.

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Originally posted by personification
    Strings are dealt with in C as pointers.
    A string is not a pointer. It just happens that people often use pointers to point to the first element of a string. That way, they don't have to be concerned about the length when pointing to its location.

    Basically what the function is doing in this case is returning a pointer to the first element of a string.

    if ( x > y )
    return ( str1);
    else if ( x < y )
    return (str2);

    if x is greater than y then it returns the memory address of the first element of str1
    else if x is less than y it returns the memory address of the first element of str2

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    entire code

    Thanx a LOT personification,

    That made a lot of things clearer; thanks....
    just so you'd know, this is the entire code

    #include <stdio.h>
    #include <string.h>

    char *a = "was--up?";
    char *b = "world!?";
    char *compare( char * , char * );

    int main ( void )
    {
    char *out;
    out = compare( a , b );

    printf(" The longer string is %s.\n", out);

    return 0 ;
    }

    char *compare ( char *str1 , char *str2 )
    {
    int x, y;

    x = strlen(str1);
    y = strlen(str2);

    if ( x > y )
    return (str1);
    else if ( x < y )
    return (str2);
    }

    I still get a warning saying:

    warning: control reaches end of non - void function
    the programme works fine but I want to know the reason of the warning;

    PS: could someone explain what casting is?

  5. #5
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078

    Re: entire code

    Originally posted by Rhodium
    warning: control reaches end of non - void function
    the programme works fine but I want to know the reason of the warning;
    That's because it is possible that x and y are the SAME in which case the function doesn't return a value, so garbage data will be returned. You need to make another return statement after

    Originally posted by Rhodium
    PS: could someone explain what casting is?
    casting is when you temporarily want value to be looked at as a datatype which it really isn't. For instance:

    let's say you had a pointer to an int and you only wanted to "grab" the first byte of it rather than the whole thing. A char is one byte in length, so you can typecast the pointer to a pointer to a char and then dereference. It will access just the first byte. Example:

    Code:
    int a,
        *ptr = &a;
    
        char b = *((char*)ptr); // stores the first byte of 'a' to b

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >casting is when you temporarily want value to be looked at as a datatype which it really isn't

    I've never really been fond of this "it lets you fake it" kind of explanation. A cast is an operator that performs a conversion.
    6.5.4 Cast operators
    • 4 Preceding an expression by a parenthesized type name converts the value of the expression to the named type. This construction is called a cast.85) A cast that specifies no conversion has no effect on the type or value of an expression.86)
    • 85) A cast does not yield an lvalue. Thus, a cast to a qualified type has the same effect as a cast to the unqualified version of the type.

      86) If the value of the expression is represented with greater precision or range than required by the type named by the cast (6.3.1.8), then the cast specifies a conversion even if the type of the expression is the same as the named type.
    Many times we encounter trivial conversions. Often this is a cast from a pointer to one object type to a pointer to another object type. An example might be converting an int* to a char*. But we should keep in mind that a "conversion" is performed nonetheless. And there are other times when the conversion is not trivial.

    -----

    While not wanting to dredge up the Casting malloc thread, there were some related comments that still bother me.Wow! Newbies wrote the C standard!This is simply not correct. An example might be casting a floating point value to a fixed point value. On one system, this conversion was implemented using a call to an assembly routine at runtime. So it had everything to do with execution.Again, this is not correct. The sizes do not need to be the same. Ever convert a char to an int? A long to a short? Between floating and fixed point (again)? Explicity or implicitly, casts (conversions) are done all the time. Do your best to be aware of this. And "used solely...", uh, no.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In the first place, there is nothing wrong with using type coercion. newbies call this 'casting'
    Who the hell are you quoting? No one in this thread said that. None of those quotes are from this thread.

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

  8. #8
    _ Munkey01's Avatar
    Join Date
    Dec 2002
    Posts
    280
    He said those quotes were from the casting malloc thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 05:58 PM
  3. Replies: 4
    Last Post: 11-23-2003, 06:15 AM
  4. Replies: 16
    Last Post: 11-01-2002, 08:28 PM
  5. Pointers, arrays , functions
    By sballew in forum C Programming
    Replies: 19
    Last Post: 09-16-2001, 11:12 PM

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