Thread: How do pointers work

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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