Thread: How do pointers work

  1. #1
    Registered User topten2016's Avatar
    Join Date
    Feb 2016
    Posts
    17

    How do pointers work

    I've started to learn pointers, but I am not sure how pointer's operations work. Can you demonstrate on this piece of code how can I use pointers in practice
    Thanx in advance!

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    
    
    void main()
    {
    int i;
    int z;
    int b = 0;
    char ch[250];
    
    
    printf("%s\n", " Enter line not longer than 250 characters");
    gets_s(ch);
    
    
    
    for (i=0; i<10; i++)
    {
    if  (i>4)
    {
    z = i;
    b++;
    break;
    }
    }
    printf("%d%c", b, ch[z]);
    }


    What I have done so far, I've tried to use pointers with the same code, get no errors, but the program suddenly feezes in the execution process

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    
    
    void main()
    
    {
        int i;
        int *i2;
        i2 = &i;
    
        int z;
        int *z2;
        z2 = &z;
    
    
    
        int b = 0;
        int *b2;
        b2 = &b;
    
    
    
    
        char ch[250];
        //char *ch2;
        //ch2 = &ch;
    
    
        printf("%s\n", " Enter line not longer than 250 characters");
        gets_s(ch);
    
    
    
        for (*i2 = 0; *i2<10; i2++)
        {
            if (*i2>4)
            {
                *z2 = *i2;
                *b2++;
                    break;
            }
        }
        printf("%d%c", *b2, ch[*z2]);
    }
    Last edited by topten2016; 03-29-2016 at 10:29 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well first of all, main returns int, not void (despite what your compiler will let you get away with)
    FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

    Second, gets_s is only "safe" if you read the manual and make a note of the fact that it takes TWO parameters.
    https://msdn.microsoft.com/en-us/library/5b5x9wc7.aspx
    You should be getting a warning about this.

    If you're trying to run code with warnings, then it's a behaviour you really need to STOP doing NOW.

    > for (i2 = 0; i2<10; i2++)
    As an example, this would be
    for (*i2 = 0; *i2<10; (*i2)++)
    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.

  3. #3
    Registered User topten2016's Avatar
    Join Date
    Feb 2016
    Posts
    17
    Thank you very much, Salem for your support
    I have fixed the mistakes. Now it works. But for some reason- as an aswer I am getting some number ( I guess address) in stead of meaning


    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    
    
    int main()
    
    {
        int i;
        int *i2;
        i2 = &i;
    
        int z;
        int *z2;
        z2 = &z;
    
    
    
        int b = 0;
        int *b2;
        b2 = &b;
    
    
    
    
        char ch[250];
        //char *ch2;
        //ch2 = &ch;
    
    
        printf("%s\n", " Enter line not longer than 250 characters");
        gets_s(ch);
    
    
    
        for (*i2 = 0; *i2<10; (*i2)++)
        {
            if (*i2>4)
            {
                *z2 = *i2;
                *b2++;
                    break;
            }
        }
        printf("%d%c", *b2, ch[*z2]);
    }

  4. #4
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    * in C is the same thing as:
    0x00(R15) in asm
    or some mcu also have @R15 or even @R15+ for the source side

    So the pointer without * in front you are changing the address it's pointing to.
    The compiler will not easily let you change that value without type-casting if doing absolute address values.

    So passing a pointer to a functions is the most useful feature, as functions will then work on offset-values
    but start location can be anywhere in memory for multiple instance of a struct for example.


  5. #5
    Registered User topten2016's Avatar
    Join Date
    Feb 2016
    Posts
    17
    For some reason

    printf("%d%c", b2, ch[z]); is returning 13892292h instead of 6h. Looks like b2 is returning the address instead of value of variable. How can I change this part?

  6. #6
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Well, ask yourself this.

    What is ch[5] equal to? You are breaking out of the for loop once i2==5

    What input are you using for your test case?
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  7. #7
    Registered User topten2016's Avatar
    Join Date
    Feb 2016
    Posts
    17
    In my loop I have *b2++; - it is calculating the value of b in increasing order
    printf("%d%c", b2, ch[z]); ch[z] - is returning what it was supposed to show, but I am talking about b2, is returning 13892292 instead of 6., the *b2 was supposed to become 6?

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Well you're still calling gets_s wrong for starters.

    After that, all bets are off.
    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.

  9. #9
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Listen to what Salem has to say and then realize that your *b2 will not equal 6 the way you have your if statement set up right now.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  10. #10
    Registered User DedicatedGamer's Avatar
    Join Date
    Mar 2016
    Posts
    21
    A pointer is kind of like a variable, except that it POINTS to a real variable's location in memory. This is generally useful when you have a large data structure that is passed to a function. If pointers to the structure were not used, the structure will be created locally with each call of the function, which can cause your computer to run out of memory, depending on what your application is doing. Here is a small example:

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    
    struct dude {
        int i;
        int z;
        int b;
        char ch[250];
    };
     
     
    int main()
     
    {
        dude *Pvar = new dude;
    
        printf("%s\n", " Enter line not longer than 250 characters");
        gets_s(Pvar->ch);
     
     
     
        for (Pvar->i = 0; Pvar->i<10; Pvar->i++)
        {
            if (Pvar->i>4)
            {
                Pvar->z = Pvar->i;
                Pvar->b++;
                    break;
            }
        }
        printf("%d%c", Pvar->b, Pvar->ch[Pvar->z]);
    }
    By the way, I don't know what your code is trying to do, so my example works, but I don't know what it does.
    Also, the 'new' operator is a c++ thing, because you were asking about pointers, and I do not know how to malloc memory..
    Last edited by DedicatedGamer; 03-29-2016 at 12:35 PM.

  11. #11
    Registered User topten2016's Avatar
    Join Date
    Feb 2016
    Posts
    17
    Thank you very much, people for your support. As a result, I started to understand better pointers.
    @Salem Could you please advise, what should I use better instead of gets_s?
    @Camel-man What input are you using for your test case? I am using just simple letters: asdfbnmjhjkjhkjh
    @DedicatedGamer Thank you very much for your C++ code example


    I am showing here a picture what I have now
    How do pointers work-exampl-png

  12. #12
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    c - What is gets() equivalent in C11? - Stack Overflow

    Check this link. The first answer explains get_s.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why does this work? (array of pointers)
    By jjohan in forum C Programming
    Replies: 5
    Last Post: 09-29-2014, 05:28 AM
  2. Replies: 43
    Last Post: 05-23-2013, 03:01 PM
  3. char pointers/app won't work
    By coffee_cup in forum C Programming
    Replies: 11
    Last Post: 01-12-2013, 05:39 AM
  4. Arrays of Pointers, how do they work?
    By yougene in forum C Programming
    Replies: 10
    Last Post: 11-16-2007, 07:58 PM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM