0% found this document useful (0 votes)
41 views10 pages

Strukture Podataka I Algoritmi 1 Vežbe 5: Lazar Vasović Nikola Bačanin Nikola Andrijević

This document contains code examples and explanations about pointers in C programming. It demonstrates how to declare and use pointers to pass arguments to functions, manipulate pointers through arithmetic, and the relationship between arrays and pointers. Functions like swap() and div_and_mod() are used to illustrate passing arguments by reference using pointers. Pointer arithmetic is applied to step through character arrays and integer arrays. Finally, it is explained that a pointer to the first element of an array can be used interchangeably with the array name itself.

Uploaded by

dragela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views10 pages

Strukture Podataka I Algoritmi 1 Vežbe 5: Lazar Vasović Nikola Bačanin Nikola Andrijević

This document contains code examples and explanations about pointers in C programming. It demonstrates how to declare and use pointers to pass arguments to functions, manipulate pointers through arithmetic, and the relationship between arrays and pointers. Functions like swap() and div_and_mod() are used to illustrate passing arguments by reference using pointers. Pointer arithmetic is applied to step through character arrays and integer arrays. Finally, it is explained that a pointer to the first element of an array can be used interchangeably with the array name itself.

Uploaded by

dragela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Kragujevac, školska 2019/20

STRUKTURE PODATAKA I ALGORITMI 1


VEŽBE 5

Lazar Vasović
Nikola Bačanin
Nikola Andrijević
POKAZIVAČI - OSNOVNI POJAM
#include <stdio.h>
main()
{
int x = 3;
int *px;
printf("Adresa promenljive x je : %p\n", &x);
printf("Vrednost promenljive x je : %d\n", x);

px = &x;
printf("Vrednost promenljive px je (tj. px) : %p\n", px);
printf("Vrednost promenljive na koju ukazuje px (tj. *px)
je : %d\n", *px);

*px = 6;
printf("Vrednost promenljive na koju ukazuje px (tj. *px)
je : %d\n", *px);
printf("Vrednost promenljive x je : %d\n", x);
}
Institut za matematiku i informatiku | 2020 | KG
SWAP: DEMONSTRACIJA PRENOSA
ARGUMENATA PREKO POKAZIVAČA
#include <stdio.h>
void swap_wrong(int x, int y) {
int tmp;
printf("x : %p\n", &x); printf("y : %p\n", &y);
tmp = x;
x = y;
y = tmp;
}
void swap(int* px, int* py) {
int tmp;
printf("px = %p\n", px); printf("py = %p\n", py);
tmp = *px;
*px = *py;
*py = tmp;
}

Institut za matematiku i informatiku | 2020 | KG


SWAP: DEMONSTRACIJA PRENOSA
ARGUMENATA PREKO POKAZIVAČA
main()
{
int x = 3, y = 5;
printf("Adresa promenljive x je %p\n", &x);
printf("Vrednost promenljive x je %d\n", x);
printf("Adresa promenljive y je %p\n", &y);
printf("Vrednost promenljive y je %d\n", y);
swap_wrong(x, y);
printf("Posle swap_wrong:\n");
printf("Vrednost promenljive x je %d\n", x);
printf("Vrednost promenljive y je %d\n", y);
swap(&x, &y);
printf("Posle swap:\n");
printf("Vrednost promenljive x je %d\n", x);
printf("Vrednost promenljive y je %d\n", y);
}

Institut za matematiku i informatiku | 2020 | KG


PRIMER 1
• Napisati funkciju koja istovremeno vraća dve vrednosti -
količnik i ostatak dva data broja.
#include <stdio.h>
void div_and_mod(int x, int y, int* div, int* mod)
{
printf("Kolicnik postavljam na adresu: %p\n", div);
printf("Ostatak postavljam na adresu : %p\n", mod);
*div = x / y;
*mod = x % y;
}
main() {
int div, mod;
printf("Adresa promenljive div je %p\n", &div);
printf("Adresa promenljive mod je %p\n", &mod);
div_and_mod(5, 2, &div, &mod);
printf("Vrednost promenljive div je %d\n", div);
printf("Vrednost promenljive mod je %d\n", mod);
}
Institut za matematiku i informatiku | 2020 | KG
POKAZIVAČKA ARITMETIKA
#include <stdio.h>
main()
{
char s[] = "abcde"; int t[] = {1, 2, 3, 4, 5};
char* ps = &s[0]; int* pt = &t[0];
printf("ps = %p\n", ps);
printf("ps+1 = %p\n", ps+1);
printf("ps+2 = %p\n", ps+2);
printf("ps-1 = %p\n", ps-1);
printf("ps-2 = %p\n", ps-2);
printf("pt = %p\n", pt);
printf("pt+1 = %p\n", pt+1);
printf("pt+2 = %p\n", pt+2);
printf("pt-1 = %p\n", pt-1);
printf("pt-2 = %p\n", pt-2);

Institut za matematiku i informatiku | 2020 | KG


POKAZIVAČKA ARITMETIKA

for (ps = s; *ps; ps++) putchar(*ps);


putchar('\n');

ps = &s[3];
printf("s = %p\n", s);
printf("ps = %p\n", ps);
printf("ps - s = %d\n", ps - s);
pt = &t[3];
printf("t = %p\n", t);
printf("pt = %p\n", pt);
printf("pt - t = %d\n", pt - t);
}

Institut za matematiku i informatiku | 2020 | KG


PRIMER (PRETHODNE VEŽBE)
• Napisati funkciju za ispis niza brojeva - demonstrira prenos
nizova brojeva u funkciju.
#include <stdio.h>
void print_array( int a[], int n) {
int i;
for (i = 0; i < n; i++) { a[i]++; printf("%d ",a[i]);}
printf("sizeof(a) - u f-ji: %ld\n", sizeof(a));

putchar('\n');
}
main() {
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int i;
printf("sizeof(a) - u okviru main : %ld\n", sizeof(a));
int n= sizeof(a)/sizeof(int);
print_array(a, n);
for (i = 0; i < n; i++) printf("%d ",a[i]);
putchar('\n');
}
Institut za matematiku i informatiku | 2020 | KG
VEZA IZMEĐU POKAZIVAČA I NIZOVA
#include <stdio.h>
void print_array(int* pa, int n);
main()
{
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int num_of_elements = sizeof(a)/sizeof(int);
int* pa;
printf("Niz a : %p\n", a);
printf("Adresa prvog elementa niza a (&a[0]) : %p\n",
&a[0]);
pa = a;
printf("Pokazivac pa ukazuje na adresu : %p\n", pa);
printf("a + 3 = %p\n", a + 3);
printf("&a[3] = %p\n", &a[3]);
printf("pa[5] = %d\n", pa[5]); printf("*(pa + 5) = %d\n",
*(pa+5));

Institut za matematiku i informatiku | 2020 | KG


VEZA IZMEĐU POKAZIVAČA I NIZOVA

printf("sizeof(a) = %ld\n", sizeof(a));


printf("sizeof(pa) = %ld\n", sizeof(pa));
print_array(a, num_of_elements);
print_array(pa, num_of_elements);
}

void print_array(int* pa, int n)


{
int i;
for (i = 0; i<n; i++) printf("%d ", pa[i]);
putchar('\n');
}

Institut za matematiku i informatiku | 2020 | KG

You might also like