Thread: Very stupid question involving pointers

  1. #1
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309

    Very stupid question involving pointers

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct
    {
        char* Name;
        char* Desc;
        //bool* Comp;
    }quest;
    int main(int argc, char *argv[])
    {
      quest *QOne;
      QOne->Name = "Number One";
      printf(QOne->Name);
      quest *QTwo;
      QTwo->Name = "Number Two";
      printf(QOne->Name,"\n",QTwo->Name);
      system("PAUSE");	
      return 0;
    }
    Everytime I run it crashes at the very start up. Can somebody explain why it does this? Almost everytime I use pointers, the program always crashes. Can somebody explain why it does this?
    (Note: I have read the tutorials)
    To code is divine

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    When you declare QOne, it contains random data. Then you deference it when it's pointing to that invalid memory address. To solve your problem you should allocate some memory and make QOne point to it. Same goes with QTwo.
    If you understand what you're doing, you're not learning anything.

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    So...why create a pointer...ever? If you’re just referencing an existing variable, what advantages does it serve? Besides allowing for quicker functions, what real purpose does it serve???

    (This thread is making me feel so dumb -_-)
    To code is divine

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >If you’re just referencing an existing variable, what advantages does it serve?
    Two words: Anonymous memory. Pointers allow you to reference memory without declaring variables first:
    Code:
    double *p = malloc ( sizeof ( double ) );
    Pointers allow you to easily create complex dynamic data structures. Without pointers you would be forced to use array indices, and that's not exactly a pleasant experience.

    >So...why create a pointer...ever?
    If you know how they work, and how to use them, you'll eventually discover the answer to this question on your own.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    its usefull when you dealing with arrays, since a function cant return an array, you use a pointer to access any element in the array
    When no one helps you out. Call google();

  6. #6
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    Quote Originally Posted by Prelude
    >If you’re just referencing an existing variable, what advantages does it serve?
    Two words: Anonymous memory. Pointers allow you to reference memory without declaring variables first:
    Code:
    double *p = malloc ( sizeof ( double ) );
    Pointers allow you to easily create complex dynamic data structures. Without pointers you would be forced to use array indices, and that's not exactly a pleasant experience.

    >So...why create a pointer...ever?
    If you know how they work, and how to use them, you'll eventually discover the answer to this question on your own.
    Ooooh thanks Prelude, I didn't know about that!

    And also, thanks to InvariantLoop for helping me with this!
    To code is divine

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ooooo fun with pointers.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
        const char *specifier[] = { "%c", "%d", "%f", "%s" };
        size_t size[] = { 1, sizeof( int ), sizeof( float ), BUFSIZ };
        int x;
        void *data = NULL;
    
        printf(
            "1) Read a single character.\n"
            "2) Read a single integer.\n"
            "3) Read a floating point number.\n"
            "4) Readin a string.\n\n"
            "Your choice: "
        );
    
    
        x = getchar();
        if( x < '1' || x > '4' )
            return !!printf( "Next time try following the rules.\n" );
    
        x -= '1';
    
        while( getchar() != '\n' );
    
        if( (data = malloc( size[ x ] )) )
        {            
            printf( "Enter your data now: " );
            fflush( stdout );
    
            if( scanf( specifier[ x ], data ) == 1 )
            {
                printf( "You entered \'" );
                switch( x )
                {
                    case 0: printf( specifier[ x ], *((char*)data) ); break;
                    case 1: printf( specifier[ x ], *((int*)data) ); break;
                    case 2: printf( specifier[ x ], *((float*)data) ); break;
                    case 3: printf( specifier[ x ], (char*)data ); break;
                }
                printf( "\'!\n" );
            }
            free( data );
        }
    
        return 0;
    }
    There's a fun example.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-04-2005, 12:17 AM
  2. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. stupid, stupid question
    By xelitex in forum C++ Programming
    Replies: 5
    Last Post: 12-22-2004, 08:22 PM
  5. Question about pointers #3
    By maxhavoc in forum C++ Programming
    Replies: 3
    Last Post: 06-19-2004, 10:45 AM