Thread: pointers to pointers within structs

  1. #1
    Registered User
    Join Date
    Aug 2003

    Angry pointers to pointers within structs

    HI Guys

    my latest program needs to access an array through a pointer within a struct by using a pointer to that struct itself. im at a loss as to how to do this. i can access non-pointer elements in the same struct (i.e bPtr->digits), i just am unsure how to access the array under my main function from within my ReadBinary function using only pointers. in the ReadBinary function also, after the scanf function has finished the while function is skipped, i beleive becuase the scanf function ends with the newline character. any ideas on how to fix this would also be most welcome. Cheers

    #include <stdio.h>
    #include <stdlib.h>
    struct BinaryRec
    	char* aPtr ;
    	int digits ;
    typedef struct BinaryRec Binary ;
    void readBinary(/*Binary* bptr*/) ;
    //void printBinary(const Binary* bptr) ;
    int main()
    Binary num ;
    char array[] ;
    Binary* bPtr = (Binary*)malloc(sizeof(Binary)) ;
    //num.aPtr = (char*)malloc(num.digits*sizeof(char)) ;
    readBinary(bPtr) ;
    return 0;
    void readBinary(Binary* bPtr)
    char x ;
    int i, n ;
    printf("Please Enter Number of Binary Digits:") ;
    scanf("%d", &n) ;
    bPtr->digits = n ;
    printf("--%d--\n", bPtr->digits) ;
    printf("Please Enter Binary Number:") ;
    while((x = getc(stdin)) != '\n')
    		if(x != 48 && x != 49)
    			printf("\nInvalid Input\n") ;
    			exit(1) ;
    			/*I Need to access the array at this point by using the pointers aPtr and bPtr to input the characters from getc 	
                           	printf("%c", x) ;
    			i++ ;
    return ;

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    struct boo bar
        char *array;
    struct foo *pbar;
    pbar = &bar;
    pbar->array = malloc( SOME_SPACE );
    ptab->array[x] = '\0';
    Is that what you're looking for?

    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Thanks, that was very helpful. i was going about it all wrong so i've managed to solve the pointer problem, although i still have the scanf problem where it skips any character input functions, even using scanf and %c itself. Any other thoughts anyone?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault with structs and char pointers
    By Keybone in forum C++ Programming
    Replies: 20
    Last Post: 01-17-2004, 01:36 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Request for comments
    By Prelude in forum A Brief History of
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. pointers and structs
    By alexir in forum C Programming
    Replies: 0
    Last Post: 10-20-2001, 01:28 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM