Thread: pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >lol Stack Overflow is cheating
    Yes, but cheating in a good way.
    My best code is written with the delete key.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Regarding Example 1.4:
    Code:
    #include <stdio.h>
    
    int main() {
        int n[5] = {1,2,3,4,5};
        int *ptr;
    
        /* points to n[0]*/
        ptr = &n[0];
        printf("ptr now points to n[%d] whose value is %d\n", (int)(ptr - n), *ptr);
    
        /* points to n[1] */
        *(ptr+1); /* this expression references n[1], but ptr remains the same */
        printf("ptr now points to n[%d] whose value is %d\n", (int)(ptr - n), *ptr);
    
        /* points to n[2] */
        (*ptr)++; /* this increments what ptr is pointing to, and ptr remains the same  */
        printf("ptr now points to n[%d] whose value is %d\n", (int)(ptr - n), *ptr);
    
        /* However, increments the value of the content of the location pointed by ptr */
        ++*ptr; 
        printf("ptr now points to n[%d] whose value is %d\n", (int)(ptr - n), *ptr);
    
        return 0;
    }
    
    /* my output
    ptr now points to n[0] whose value is 1
    ptr now points to n[0] whose value is 1
    ptr now points to n[0] whose value is 2
    ptr now points to n[0] whose value is 3
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Alright,

    Thanks guys. I will edit my original post and make the necessary changes.

    Though, I had a late night last night and started to slip up on my presentation. My apologies. I occasionaly fix stuff. Little things I keep skipping over, and adding [color] tags too. Thanks Salem for pointing that out!

    Note: All examples have been modified. And can now compile sucessfully with -Wall, -pedantic, -ansi, and -std=c89 GCC compiler flags. Remember, I made a few examples of what not to do, so of course those may return warnings or errors.


    - Stack Overflow
    Last edited by Stack Overflow; 02-04-2005 at 12:19 PM.
    Segmentation Fault: I am an error in which a running program attempts to access memory not allocated to it and core dumps with a segmentation violation error. This is often caused by improper usage of pointers, attempts to access a non-existent or read-only physical memory address, re-use of memory if freed within the same scope, de-referencing a null pointer, or (in C) inadvertently using a non-pointer variable as a pointer.

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, 10:33 AM
  4. Staticly Bound Member Function Pointers
    By Polymorphic OOP in forum C++ Programming
    Replies: 29
    Last Post: 11-28-2002, 01:18 PM