Problem with the pointer

This is a discussion on Problem with the pointer within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to find a reason why my program doesn't work. It is the last challenge in chapter ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Location
    Poland
    Posts
    2

    Problem with the pointer

    Hello, I am trying to find a reason why my program doesn't work. It is the last challenge in chapter 9 from "C Programming for the Absolute beginner".
    When I add a name, and want to print it later (using function printBook) my ptrC is always 0, although I am incrementing it in function addEntry? What I am doing wrong?
    Thanks for helping


    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    
    typedef struct phonebook { 
        
        char name[10];
        int number;
        
    } pb; 
    
    
    void printBook(pb *, int *);
    void addEntry(pb *, int *);
    
    
    
    
    main()
    {
        
        int counter = 0;
        pb pb1[5] = {0,0,0,0,0};
        
        int iSelection;
        
        
        while ( iSelection != 3 ) {
          //system("clear");
          printf("\n****Phone Book ****");
          printf("\n\n1\tAdd new entry\n");
          printf("2\tPrint out the book\n");
          printf("3\tQuit\n");
          scanf("%d", &iSelection);
    
    
          switch (iSelection) {
    
    
          case 1:
             addEntry(pb1, &counter);
         printf("\n%d", counter);
             break;
    
    
          case 2:
             
         printBook(pb1, &counter);
             break;
    
    
          }  //end switch
    
    
       }  //end loop
       
       printf("\nBye!\n");
        
    
    
        
    } //end main
    
    
    
    
    void addEntry(pb * pb2, int *ptrC)
    {
        char name[10];
        char *n;
        
        if ( *ptrC < 5){
        printf("\nAdd a name: ");
        n = gets(name);
        gets(name);
        strcpy(pb2[*ptrC].name, n);
        
        printf("\n%s", pb2[*ptrC].name);
        printf("\n%d", *ptrC);
        
        //printf("\nAdd a number: ");
        //scanf("%d", pb2[*ptrC].number);
        
        *ptrC++;
        } // end if
        
        else
        printf("\nPhone book is full!");
        
        
    }
    
    
    void printBook(pb * pb2, int *ptrC) {
        
        int x;
        
        if (*ptrC != 0 ) {
        
            for ( x = 0; x < *ptrC; x++) {
        
            printf("\nName: %s\n", pb2[x].name);
            printf("Number: %d\n", pb2[x].number);
            
        
        } //end loop
        
        } //end if
        
        else
        printf("\nNo entries!");
        
    }

  2. #2
    Registered User
    Join Date
    Mar 2013
    Location
    Bangalore
    Posts
    4
    In addEntry() replace
    *ptrC++ with (*ptrC)++; or *ptrC = *ptrC + 1;

    -Yogi
    My Blog &mdash; Blog by Yogindar

  3. #3
    Registered User
    Join Date
    Mar 2013
    Location
    Poland
    Posts
    2
    Thanks, it works

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,628
    Can you explain WHY it worked? If you can't, you'll be doomed to making similar mistakes in future.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointer to a pointer variable
    By Rodri in forum C Programming
    Replies: 2
    Last Post: 11-20-2011, 10:50 AM
  2. pointer to pointer realloc problem
    By prakash0104 in forum C Programming
    Replies: 14
    Last Post: 04-06-2009, 09:53 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 10:40 AM
  4. Replies: 4
    Last Post: 11-05-2006, 02:57 PM
  5. pointer to pointer how do i solve following problem?
    By kobra_swe in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 05:49 PM

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