Thread: Newbie question (Pointers)

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    9

    Newbie question (Pointers)

    Hi everyone, i have a question that i cannot answer. Consider the following declaration:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char (*PArray)[256];
    char *p;
    
    int main()
    {
        ...
        return 0;
    }
    I don't understand the declaration of PArray. I think that is a pointer to an array of char, but i can't acces any of the individual componentes. How can i do that?
    Thanks a lot!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It is indeed a pointer to an array of char of 256 elements. Each increment of that pointer would skip to the next array.
    Use a pointer to char to point to the first element in the array. When incrementing it, it will step forward to the next element, because that's how pointers work. They step forward 1 element.
    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.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Ok. If i do,

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char (*PArray)[256];
    char *p;
    
    int main()
    {
        p = PArray;
        *p = 'a';
        printf("%c \n", *p);
        return 0;
    }
    Does not work. What i'm doing wrong?
    Could i use the index of the array (ie *PArray[2])?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    http://cpwiki.sourceforge.net/Common...llocating_them
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	char b[256];
    	char (*PArray)[256] = &b;
    	char* p = &*PArray[0];
    	*p = 'a';
    	printf("&#37;c \n", *p);
    	return 0;
    }
    Works as expected.

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Why do you declare array b?
    I think i understand that PArray is a Pointer to an array of 256 components. I mean, in the declaration the compiler allocate 256 bytes of memory. I don't see the need of declaring a new array of 256 components...
    Last edited by Tux_Fan; 11-28-2008 at 08:42 AM.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, it does not allocate anything. It is a pointer. See the article I referenced.
    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.

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    Ok, then why if i do

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    char (*PArray)[256];
    char *p;
    
    int main()
    {
        p = &PArray;
        *p = 'H';
        *p++;
        *p = 'i';
        *p++;
        *p = '!';
        *p = '\0';
        p = &PArray;
        printf("&#37;s \n", p);
        return 0;
    }
    Works fine. Sorry if it's a stupid question but i really want to understand what is happening.
    Last edited by Tux_Fan; 11-28-2008 at 08:55 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I believe that you are just lucky (or unlucky, as the case may be): as far as I can tell, you are dereferencing a null pointer when you write *p since p is statically allocated, hence it starts off as a null pointer.

    Actually, why are PArray and p global variables?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You would like that, don't you?
    That code is horribly broken it won't even compile on a C++ compiler.
    It is undefined behavior x1000.

    And for the record, it crashes for me.

    Time for pointer materials (again):
    http://cslibrary.stanford.edu/104/
    http://cboard.cprogramming.com/showp...3&postcount=31
    http://cboard.cprogramming.com/showp...65&postcount=4
    http://cpwiki.sourceforge.net/A_pointer_on_pointers
    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.

  10. #10
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    I have edit the code, in the first assigment has an error. That code compile with gcc without any problem and returns the desired result. It's only an example. I only want to know what is happening, because of that i post NEWBIE, i'm not an expert.
    Thanks a lot for your help.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    p = &PArray;
    Assigns the address of the actual pointer to p. Likely not what you want.
    And, it... should... not... WORK!

    *p = 'H';
    Stores 'H' in the first byte of the pointer PArray.

    *p++;
    Dereferences p (no effect), then increments the pointer p by one byte, to point to PArray + 1.

    *p = 'i';
    Assigns 'i' to PArray + 1.

    *p++;
    Same as above. Now points to PArray + 2.

    *p = '!';
    Assigns '!' to PArray + 2.

    *p = '\0';
    Assigns '\0' to PArray + 2.

    p = &PArray;
    Same as before.

    Do you pay heed to warnings at all? You should get a multitude of warnings.
    Try printing the value of PArray at the end of the program, and at the beginning:

    Code:
    printf("&#37;p", PArray);
    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.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    9
    You were right, that's horrible. Now i understand a little more. I don't have any warnings, and all compiler warnings are enabled!
    I'm replacing the value of the pointer, and the consecutive bytes, with chars! What a mess!
    Now, i don't understand why i have to declare that PArray is a pointer to an array of 256 components.
    Why is the difference if i declare PArray like a pointer to an array of char?

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Tux_Fan View Post
    You were right, that's horrible. Now i understand a little more. I don't have any warnings, and all compiler warnings are enabled!
    I'm replacing the value of the pointer, and the consecutive bytes, with chars! What a mess!


    Now, i don't understand why i have to declare that PArray is a pointer to an array of 256 components.
    Why is the difference if i declare PArray like a pointer to an array of char?
    The problems with your code was as follows:

    In the first one, your created a pointer to an array of chars, with 256 elements, but again, you only created the pointer, so it pointed to nothing (did you see the pointer tutorial movie?).
    So in my example, I created a local array and set the pointer to point to it. So it works.

    In your second example, it worked because you used the PArray pointer as a buffer. If you would have done p = &*PArray[0], and then used the code as usual, it would crash, and then it would be equal to the same code as before.

    It all boils down to that you must have a storage first. A pointer is not a storage - it is merely a variable pointing to something, whether that is valid or not.
    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
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Tux_Fan
    I don't have any warnings, and all compiler warnings are enabled!
    That is weird. What version of gcc are you using?

    Quote Originally Posted by Tux_Fan
    Now, i don't understand why i have to declare that PArray is a pointer to an array of 256 components.
    You don't. It depends on what you want to do.

    Quote Originally Posted by Tux_Fan
    Why is the difference if i declare PArray like a pointer to an array of char?
    What do you mean?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by Elysia View Post
    http://cpwiki.sourceforge.net/Common...llocating_them
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    	char b[256];
    	char (*PArray)[256] = &b;
    	char* p = &*PArray[0];
    	*p = 'a';
    	printf("%c \n", *p);
    	return 0;
    }
    Works as expected.
    Is it okay to simply write that as,
    char* p = PArray[0];
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  2. Question about the introduction to pointers lesson
    By Freemind11 in forum C++ Programming
    Replies: 11
    Last Post: 02-15-2006, 11:45 PM
  3. Question on pointers?
    By robasc in forum C++ Programming
    Replies: 16
    Last Post: 12-25-2005, 01:15 AM
  4. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM
  5. very newbie question: how to copy files
    By webwesen in forum C Programming
    Replies: 26
    Last Post: 04-25-2002, 03:01 PM