Results 1 to 8 of 8

Thread: pointer confusion

  1. #1

    pointer confusion

    I am confused about functions that return pointers and was woundering if someone can help clerify..

    I have not programed anything in C in about 4 months, recently I decided to start a project.

    I have a book Teach yourself C in 21 days, which I have read long time ago, and am now useing for refernce.

    There are 2 chapters that talk about pointers.

    Here is my problem, I was looking for a function that will split a string, simular to split in perl.

    After useing google I was able to find a function that someone wrote that will do what I want it to do. The function returns a pointer to a pointer of type char.

    Forgeting about that function I will expain what I understand about pointers and what I dont quite understand:

    I understand that initialy you must declare a pointer like so

    int *ptr;

    and then it must be initialized to point to something

    int line = 25;
    ptr = &line;

    where &line is the address in memory.

    And then to use the pointer you use the indirection operator * to access the pointed to value

    fprintf(stderr,"There was an error on line %d",*ptr);

    now as an example of what I dont understand about functions.

    if I have a function such as:

    char *strchr(char *str, int ch);

    which returns a pointer to type char, and I declare some stuff:

    char *loc, buf[25];
    int ch;

    and I want loc to contain the character pointed to;

    which of the following is corect and why:

    loc = strchr(buf, ch);
    ---OR---
    *loc = strchr(buf, ch);

    I know that loc is suposed to be the address in memory of the value pointed to, and *loc is refernceing the actual memory value.

    So if strchr returns a pointer to the character it found why are we assigning that to loc whereas we should be assigning that to the valuse pointed to by *loc

    strchr does not return the address therefor
    loc = strchr(buf,ch);

    would be incorect???

    I hope I have made it clear what I dont understand and can someone please explain to me how I access the character pointed to by the return value of the function.
    test

  2. #2
    The Iceman Cometh
    Join Date
    Aug 2001
    Posts
    1,209
    It's been a while since I have had to return pointers from functions, and the classes I teach don't use pointers (they're covered in later courses), but, if I remember right, you should be able to do:

    [CODE[loc =strchr(buf, ch);[/CODE]

    You can only do this, however, if you have delcared loc as a pointer, not as a character. If I'm wrong on that (sorry, I use pointers quite a bit in projects, but I never return them because I work more with various types of lists, etc.), I apologize and hopefully someone will clarify it. I'll look it up when I get off campus today.

    AJ

  3. #3
    My book says the same thing, that loc = strchr(buf,ch);
    is the corect way, but it does not make sence, because I was taught that loc contains the address and *loc contains the value (in this case the character)

    the actual function I have found to do the spliting has a prototype of

    char **stringSplit (char *line, int feild_cound,char delim);

    where delim would be the delimiter, in my case a space

    char delim = " ";

    I will have a string such as

    dir /B >> text.txt

    So I was useing the example in my book for a refernce, seeing that the only difernce between the two return types is one is a single pointer while the other is a double.

    Here is what got me confused:

    char *loc, buf[80];
    int ch;

    printf("Enter the string to be searched: ");
    gets(buf);
    printf("Enter the character to be searched for: ");
    ch = getchar();

    loc = strchr(buf,ch);

    if (loc == NUL)
    printf("The character %c was not found.",ch);
    else
    printf("The character %c was found at position %d.\n",ch,loc-buf);


    ==========

    loc-buf makes sence because if loc is the address in memory and buf is an addres in memory and you substract the two you would get the difernce in bytes, and since a char is 1 byte, you would get the amount of characters from the start of the string in buf where ch was found.

    Simulary it should be posiible to use sizeof(buf) ext...

    But if loc is an address in memory why would we be assigning the return of strchr to a variable that is suposed to store a address of a char, not the actual char itself?
    test

  4. #4
    Senior Member
    Join Date
    Nov 2001
    Posts
    257
    ptr = function();

    Is correction if the function definition is: char * function()

    Here's why:

    While *ptr is the value that the pointer points to, ptr is still a variable that contains a memory location. The function call doesn't exactly "return a pointer" so much as it returns a memory location to be stored in a pointer variable.

    It's like when a function has a pointer as a argument, you don't actually have to pass it a pointer (though you can), you can pass it a variable of the same type with the & to denote you are passing it's memory location instead of it's value.

    This can be very useful to keep memory useage down in your program, write functions that use a constant pointer, and pass the variable by reference, instead of passing by value. This way it looks to the same memory location (ie it doesn't use double the memory), but you maintain the protection of the function not being able to change the value.

    Edit: the function strchr has the following prototype: char * strchr(const char *, int), so it returns a memory location and not the actualy character value.
    -Shkuey
    Living life one line of error free code at a time.

  5. #5
    Senior Member
    Join Date
    Jun 2002
    Posts
    165
    i'll try to simplify the clarity of pointers.

    when you declare a pointer

    char *p;

    the pointer declaration(*) should be assumed to be a part of the type specification.

    so after declaration, (p), by itself, is a pointer. (*p), without being cast, is a character. if you have a function that returns a pointer (ie strchr) you want to assign it to the pointer p, not the character *p.
    -droby10

  6. #6
    Thank you, that makes it clear now, I think I got confused about the way my book discribed the function,

    Definition of strchr from my book:

    The function strchr() searches str from left to right until the character ch is found or the terminateing null character is found. If ch is found, a pointer to it is returned. If not, NULL is returned.
    But now that you have fully explained it I understand now. Thank you everyone.
    test

  7. #7
    Senior Member
    Join Date
    Jun 2002
    Posts
    394
    you have said you understand now, but i must comment on this,
    I have a book Teach yourself C in 21 days, which I have read long time ago, and am now useing for refernce.
    i would advise you to use a reference book as a reference book, this should prove invalueable to you. although i am stating the obvious, im not being smart of sarchastic. you will get alot more done with one of those, and it should be more useful to you if you already are familiar with the language, whatever the language.
    Hmm...theres something a little peculiar here. Oh i see what it is! the sentence is talking about itself! do you see that? what do you mean? sentences can\'t talk! No, but they REFER to things, and this one refers directly-unambigeously-unmistakably-to the very sentence which it is!

  8. #8
    Senior Member
    Join Date
    Sep 2001
    Posts
    535
    hello aj , what i suggst is first try programming small small programs which uses functions returning pointers and things like that .. and then go for complex declarations.. for that initially read book which are meant for newbies .. if directly u jump to unleased versions that u will not understand much.. as teh approach is not easy.. but they are bit advanced...

    intruder
    A laptop, internet connection and beer.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •