Pointers

This is a discussion on Pointers within the C Programming forums, part of the General Programming Boards category; I came across this Code: 1. int strlen(char *s) 2. { char *p = s; 3. 4. while (*p != ...

  1. #1
    1337
    Join Date
    Jul 2008
    Posts
    135

    Pointers

    I came across this

    Code:
    1.  int strlen(char *s)
    2.	   { char *p = s;
    3.
    4.				 while (*p != \0);
    5.				   p++;
    6.				 return p-s;
    7.		   }
    Questions:
    1. In line 1, why the declaration of the *s is *s instead of s[]?
    2. In line 2, why the initialisation of pointer p (*P) is assigned to s instead of *s? Since *p is to be filled with values and *s is a value and not 's' which is an address of pointer s.
    3. In line 4, why there is a semicolon after the braces of while?
    isnt it supposed to be
    Code:
                while (*p != \0)
    {
    			             p++;
    }
    				 return p-s;
    4. This is a general question about pointer. If i declare a pointer as int *p, how many address will it increase if i type p+1? Will it be 1 or 4?

    Thank you.
    Last edited by valthyx; 07-24-2009 at 04:54 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    1. In a function prototype, arrays are converted to pointers anyway so it doesn't matter.
    2. Because s is a value, specifically the value you want p to have (so that p points to the same place s does).
    3. Because you typed it incorrectly.
    4. It depends on what you mean by "address". The value of the pointer will go up by sizeof(int) probably four -- which is going up by one "int address".

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    1. In line 1, why the declaration of the *s is *s instead of s[]?
    Arrays and pointers are interchangable. If you have a parameter s[], it will expect the address of an array which is a pointer. Hence you can write *s instead.

    2. In line 2, why the initialisation of pointer p (*P) is assigned to s instead of *s? Since *p is to be filled with values and *s is a value and not 's' which is an address of pointer s.
    To declare a pointer you must use the * operator to tell the compiler that it is a pointer. So, over here char *p is declared as a pointer to char. s is already passed in as a pointer so you can simply assign s to p.

    's' is not the address of the pointer s, &s is the address of the pointer s. s is simply a pointer to char hence it is a string.
    3. In line 4, why there is a semicolon after the braces of while?
    I believe that is a typo which will result in an infinite loop.

    4. This is a general question about pointer. If i declare a pointer as int *p, how many address will it increase if i type p+1? Will it be 1 or 4?
    That depends on the type of pointer - char will be 1 byte , int - 4 bytes. However if you add 1 to an array, you are guaranteed to get the next element as long as you are within bounds.

  4. #4
    1337
    Join Date
    Jul 2008
    Posts
    135
    Thanks guys, but i still have one doubt.

    for example,
    Code:
    int getstring (int *s) //where *s will take a string. Does it make sense that  " s = "hello"?
    That simple means that "hello" is replacing the address of s.

    as far as i know, s is used in this way, s = &a , where &a is an address in memory.

    Please explain, i would appreciate if you guys could explain to me as i am new to C.
    Last edited by valthyx; 07-24-2009 at 11:21 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It does not make sense for a string to be represented by an int*. A char*, yes, but not an int*. (And since "hello", if it appears in your program, is a character array, then yes, that would be passed to a function as a pointer just like every other array.)

  6. #6
    1337
    Join Date
    Jul 2008
    Posts
    135
    So, you mean this is correct?

    int getstring(char* s) // the function gets the input from the user
    s = ("The string you entered is %s\n", s)
    printf ("%s", s); //this will print ""The string you entered is......"
    Is this correct?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No. If you had stopped at
    Code:
    printf("The string you entered is %s\n", s);
    you'd have something. It wouldn't match the comment "the function gets the input from the user" though.

  8. #8
    1337
    Join Date
    Jul 2008
    Posts
    135
    Or should i put this:

    int getstring (char* *s) ?

    second question, why do we have **(2 asterisks) in "int main(int argc, char **argv) ?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because argv is a big ol' pile of strings, not just one string. (Hence the alternative notation "char *argv[]" -- an array of strings.) Unlike your s.

  10. #10
    1337
    Join Date
    Jul 2008
    Posts
    135
    char* p;

    p = "hello";

    why "hello" string could be assigned to p which stores only addresses?

    Should "hello" string be stored in *p instead of p as *p is where the values to be stored?

    Thank you, and i would be very appreciated if anyone explains this to me

  11. #11
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    All string literals in C are represented internally as character arrays, terminated by the null terminating string '\0'. But, when you declare a pointer to char and assign it the value "hello", it creates a block of memory somewhere in code with the respective chars and returns the address of that memory. You can modify the pointer as long as it contains a string literal.
    Code:
           char *p;
    	p = "hello";
    	p = "goodbye";

  12. #12
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    here is an article which explains it better - section 5.5: Character Pointers and Functions

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Quote Originally Posted by valthyx View Post
    why "hello" string could be assigned to p which stores only addresses?

    Should "hello" string be stored in *p instead of p as *p is where the values to be stored?

    Thank you, and i would be very appreciated if anyone explains this to me
    Think about what you're saying.
    You know that a string is composed of several characters.
    And you know that if p is char*, then *p is char, so how can you store several characters in one char?

    Furthermore, a string literal, like above, cannot be modified. Therefore, when assigning string literals, always use const char*.
    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.

  14. #14
    1337
    Join Date
    Jul 2008
    Posts
    135
    What is char* by the way?

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,777
    Sigh. A pointer to char. Do you really understand pointers?
    It seems like you're just guessing.
    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.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using pointers to pointers
    By steve1_rm in forum C Programming
    Replies: 18
    Last Post: 05-29-2008, 05:59 AM
  2. function pointers
    By benhaldor in forum C Programming
    Replies: 4
    Last Post: 08-19-2007, 10:56 AM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 12:18 PM

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