#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0

    Pointer Mishap :\


    Hey guys :D
    I recently saw my mum picking out photos and painfully writing the serial numbers on her phone,so i decided to make a program that makes it easieer for her...
    The program seems to be running fine...but in the end,where it has to printf the serial numbers from the pointer,it only remembers the last number and then the ones before it go all 200
    84568

    Here is the code :



    Code:
    /*This program has been made for my mom to select photos at peace*/
    #include <stdio.h>
    #include <conio.h>
    
    int main()
    {
        unsigned int starting;
        unsigned int input = 0;
        unsigned int product;
        unsigned int*product_pointer;
        unsigned int i = 0;
        int j = 1;
        int m;
        int k = 1;
        printf("\nWelcome to the PhotoSelector\nAll you have to do is enter your first digits\nE.g 400 \nSo if you want 409, Yout type in \n400 \nas the starting code and then\n9\nIf you want 469,you can do it the same way or like \n460\n9\nRemember : To reset your starting num type in '404'\nNow,please enter your starting num\nLastly,to view all your selections type in ' 101 '\n>");
        scanf("%u",&starting);
        while(1 < 2)
        {
        while(starting != 101 && input == 404)
        {
            printf("Please enter the ne starting num\n>");
    
        }
        while(starting != 404 && starting != 101 && input != 404 && input != 101)
        {
            printf("Please enter the subnumber of the photo you like\n> ");
            scanf("%u",&input);
            if(input != 101 && input != 404)
            {
                product = starting + input;
                if(i == 0)
                {
                    product_pointer = &product;
                     product_pointer = product_pointer + 1;
                                 i++;
                }
                else if(input != 101 && input != 404)
                {
    
                    product_pointer = &product;
                     product_pointer = product_pointer + 1;
                                 i++;
                }
    
            }
    
        }
        m = i;
        while(input == 101 && i > 0 && j != m && j <= m)
        {
            if(k == 1)
            {
                printf("The photos you liked are :");
                k++;
            }
    
            printf("\n %u ,",*(product_pointer - j));
            i--;
            j++;
            getch();
        }
    
    
     return 0;
        }
     return 3;
    }
    TY guys :)
    :D


    P.S Guys feel free to download this and compile it,so it is easier for u to understand hat i am saying :)
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    > unsigned int product;
    > unsigned int*product_pointer;
    You only have one product.

    > product_pointer = &product;
    Which you point at.

    > product_pointer = product_pointer + 1;
    And then immediately make it point to something that doesn't exist.

    > printf("\n %u ,",*(product_pointer - j));
    And then this blows up for some j which isn't equal to 1 (the only value which would offset the +1 you added earlier).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by salem
    > unsigned int product;
    > unsigned int*product_pointer;
    You only have one product.

    > product_pointer = &product;
    Which you point at.

    > product_pointer = product_pointer + 1;
    And then immediately make it point to something that doesn't exist.

    > printf("\n %u ,",*(product_pointer - j));
    And then this blows up for some j which isn't equal to 1 (the only value which would offset the +1 you added earlier).
    I actually wanted to make an array first,but thinking that they have limited storage and i can keep incrementing the pointers storage,i thought i should do that..
    What do you suggest i do?
    TY :)
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    I suggest you make your array version first, so you have something that works.

    If you have an existing fixed array code that works, then extending it to use allocated memory of variable size is very easy.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by salem
    I suggest you make your array version first, so you have something that works.

    If you have an existing fixed array code that works, then extending it to use allocated memory of variable size is very easy.
    I didnt really understand :\
    I dont really know HOW to increase the size of the array...
    (BTW sorry if i am being dumb,and understood what u just wrote :( )
    TY :D
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    A fixed array
    Code:
    #define LEN 10
    int array[LEN];
    for ( i = 0 ; i < LEN ; i++ ) {
        // do something
    }
    Can be turned into
    Code:
    int len = 10;
    int *array = malloc( len * sizeof(*array) );
    for ( i = 0 ; i < len ; i++ ) {
        // do something
    }
    You don't have to write the whole code in one sitting (nobody sat in a room, typed solidly for 3 months and out popped a working version of firefox). Break the problem down into achievable steps.

    One simplification is to just use an array instead of managing memory yourself.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    7
    Rep Power
    0
    Originally Posted by salem
    A fixed array
    Code:
    #define LEN 10
    int array[LEN];
    for ( i = 0 ; i < LEN ; i++ ) {
        // do something
    }
    Can be turned into
    Code:
    int len = 10;
    int *array = malloc( len * sizeof(*array) );
    for ( i = 0 ; i < len ; i++ ) {
        // do something
    }
    You don't have to write the whole code in one sitting (nobody sat in a room, typed solidly for 3 months and out popped a working version of firefox). Break the problem down into achievable steps.

    One simplification is to just use an array instead of managing memory yourself.
    Cool :)
    Thanks a lot,but I was wondering(just wondering,u dont need to waste your time demonstrating) is it possible to use the pointer as an endless array?
    Thanks a lot :D
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,387
    Rep Power
    1871
    There's no such thing as an endless array.

    Through malloc and realloc, you can have a variably sized array you can expand and shrink within the physical limits of the memory available to programs on your system.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo