Thread: What does this prototype mean?

  1. #1
    Registered User
    Join Date
    Apr 2002

    Question What does this prototype mean?

    I am learning strings..and I came across this prototype for the strncpy function..can someone please explain..

    char *strncpy(char *to_string, const char *from_string, int size);

    I understand most of it..except..why is strncpy of type pointer to a character?? and what does const (??) char*from_string mean??
    Why is it a constant? Is it cos it doesn't change? would I type in const??
    Thanks in advance

  2. #2
    train spotter
    Join Date
    Aug 2001
    near a computer
    It means it returns a pointer to a string.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  3. #3
    Registered User stautze's Avatar
    Join Date
    Apr 2002
    >char *strncpy(char *to_string, const char *from_string, int size);

    You are just copying at most size characters from from_string to to_string. If size is bigger than the string, you get a '/0' as the last character in yout to_string.

    The return value is pointer to the first character in the to_string.

    The const declaration means that the string from_string is not changed by this particular function. Note that this is different than using it when declaring a variable.

    But you don't have to take my word for it, look in any C book. I use K&R as my bible.
    'During my service in the United States Congress, I took the initiative in creating the Internet.' - Al Gore, March 9, 1999: On CNN's Late Edition

  4. #4
    Registered User
    Join Date
    May 2002
    i will try to explain your query this way;
    char *strcpy(char *to_string, const char *from_string);
    in above line is the declaration of the function strcpy;
    normally fuctions are used to return value of the type they are declared
    in this case char is its type.which means that it can only return character
    or char type of values.
    putting * in front of strcpy has just made it possible for the function
    to return the address of the char(character)type variables or const;which means this function will not return any value but only the value which will be an address of some character type variable;any value going out from this function will be comprehended as the address of some character variable;which
    makes one more thing important that the value returned from this
    function could only be recieved by an char pointer for example
    char *resultant_string;
    char *put_into_this;
    char *this_string;
    resultant_string=(char *)malloc(sizeof(this_string));
    resultant_string=strcpy(put_into_this,this_string) ;
    ofcourse others would also be able to recieve the value returned by
    this function but through typecasting;
    a little more detail to it
    (a).int num;//this means num is a integer type variable which in turn means it can hold only integer type values like 1,2,3,4
    but it cannot hold a,b,c,d;now if it is written like this
    (b)int *num;//this would mean yes still num is an integer variable but now its purpose is diffrent and that purpose is to hold the address of integer type variables;now any integer value passed to it would be comprehended by the compiler as the address of some integer variable in the memory;
    suppose you write num=1; in first case that is(a)
    it would mean integer value 1;
    now you write num=1;in second case that is(b)
    it would mean memory location 1;
    in either case we can say num is holding a value 1;
    but only the diffrence is in one case it is simple 1;
    and in other case it is memory location numbered 1;
    same logic is applied to functions like char *strcpy(char *to_string, const char *from_string);
    which means whatever value would be held by char *strcpy in it`s return argument would be some memory location and that memory location will be for
    some character variable unless ofcourse typecasted;
    now what is CONST doing here;
    CONST has just made it cumpulsary for the function that no
    statement or argument in side the function should dare change or manipulate
    the value of *from_string;it is as simple as that if some statement like
    from_string=scr1 is written or from_string='a' any statement which is trying to
    change the original value of scr it would result in an error;
    ofcourse there are ways to do this but they are through pointers not
    i think this would do for explaning your problem.
    if i am wrong somewhere please dont mind i am just a student
    i myself need the help of your esteemed self.


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  2. Function prototype questions
    By Kayoss in forum C++ Programming
    Replies: 6
    Last Post: 11-30-2005, 05:27 PM
  3. prototype poblem
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 10-30-2005, 10:01 AM
  4. Prototype syntax for sub-class constructor
    By starkhorn in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2004, 07:33 AM
  5. call to function without prototype
    By lgbeeb in forum C Programming
    Replies: 2
    Last Post: 03-25-2003, 12:20 PM