UIU: Learn C by Examples : Pointers


Problem 1


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main () 
{
    int a = 10;

    int *p = &a;

    printf ("%d %d\n", a, *p);

    (*p)++;

    printf ("%d\n", a);

    ++(*p);

    printf ("%d\n", a);

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 2


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main () 
{

	int array [5] = {5, 4, 3, 2, 1};

	int *pointerVariable;

	pointerVariable = array;

	printf ("%d", *pointerVariable);

	printf ("%d", ++*pointerVariable);

	printf ("%d", *++pointerVariable);

	printf ("%d", ++*++pointerVariable);

	return 0;

}

// @END_OF_SOURCE_CODE

Problem 3


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main () 
{
	int *p, i;

	i = 5;

	p = &i;

	printf ("%d", *p);

	printf ("%d\n", i**p);

	printf ("%d\n", i**p*i+*p);

	return 0;
}
// @END_OF_SOURCE_CODE

Problem 4


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main () 
{
	int a, b, c, *p, *q, *r;

	a = 10;     b = 20;     c = 30;

	p = &a;     q = p;

	printf ("%d\n", *q);

	(*p)++;

	printf ("%d\n", *p);

	printf ("%d\n", *q);

	r = q;

	r++;

	printf ("%d\n", *q);

	r--;

	printf ("%d\n", *r);

	(*q)++;

	printf ("%d\n", *r);

	return 0;
}

// @END_OF_SOURCE_CODE

Problem 5


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

void changeValue(int *p)
{
    (*p)++;
}

int main ()
{
    int a = 10;
    int b = 20;

    changeValue(&a);

    printf ("%d %d\n", a, b);

    changeValue(&b);

    printf ("%d %d\n", a, b);

    return 0;
}

// @END_OF_SOURCE_CODE

Problem 6


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

void changeValue (int *p, int q)
{
	q = *p + 1;
	(*p)++;
	p++;
}

int main ()
{
	int p, q;
	p = 10;     q = 20;

	changeValue(&p, q);

	printf ("%d %d\n", p, q);

	changeValue(&q, p);

	printf ("%d %d\n", p, q);

	return 0;
}

// @END_OF_SOURCE_CODE

Problem 7


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main () 
{
	int array [5] = {5, 4, 3, 2, 1};

	printf ("%d\n", *array);

	printf ("%d\n", *array + 1);

	printf ("%d\n", *(array + 2));

	return 0;
}

// @END_OF_SOURCE_CODE

Problem 8


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main () {

    int *p, **q;

    int a = 10;

    p = &a;

    q = &p;

    (*p)++;

    (**q)++;

    printf ("%d\n", a);

    return 0;
}

// @END_OF_SOURCE_CODE

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s