You are here:

C/C programming

Advertisement


Question
What gets printed and why?

#include <stdio.h>

void change_values(int a[], int b, int* c);

int main(void) {
int x[] = {1, 2, 3};
int y = 3;
int z = 2;
printf("{%d, %d, %d} %d %d\n", x[0], x[1], x[2], y, z);
change_values(x, y, &z);
printf("{%d, %d, %d} %d %d\n", x[0], x[1], x[2], y, z);
return 0;
}

void change_values(int a[], int b, int* c) {
while(--b > 0)
a[b] += *c;
*c = 0;
}

Answer
Hi Murat,

By adding some printf statements to "change_values" we can see the changes to x, y, and z. The value of y does not change after returning from change_values.


x ={1, 2, 3}, y = 3, z = 2;
//enter change_values, change_values(x, y, &z);
y -= 1;
y = 2;
x[2] += 2;
y -= 1;
y = 1;
x[1] += 2;
z = 0;
//exit change_values
x ={1, 4, 5}, y = 3, z = 0

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tony Nazzal

Expertise

I will answer any c language programming and syntax questions.

Experience

I have almost a decade of c programming experience.

Education/Credentials
I have a bachelor of science degree and several years of industry experience.

©2016 About.com. All rights reserved.