Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My teacher gave us a question in programming class today and I don't understand how he got the answer. I was hoping someone could explain it to me. We basically have to show what the programs output would be, however I am somewhat confused as to how to get the answer to the question. The Question is as follows:
#include <stdio.h>
void do_something (int , int * );
int main (void)
{
int first = 1, second = 2 ;
do_something(second, &first);
printf("%4d%4d\n", first, second);
return (0);
}
void do_something (int thisp, int *that)
{
int the_other;
the_other = 5;
thisp = 2 + the_other;
*that = the_other * thisp;
return;
}
Answer
35 and 2
The function do_something contains 2 parameters.
normal integer (thisp)
pointer to an integer. (that)
What your teacher wanted you to learn is, pass by value and pass by address.
In pass by value, the original value doesn't change. This is because in the example given.
value of variable second is copied thisp variable.
In pass by address, the original value can be modified within the function.
This is because, the pointer that is pointing to the location of variable first. So if value of that is changed, the value of first will also change.
This is why, value of first is changed in the output and value of second is unaffected.
thisp = 2 + the_other;
*that = the_other * thisp;
Means:
thisp = 2 + 5
*that = 5 * 7
And that contains address of first in main which is overwritten in do_something as 35. Second remains 2.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have the following C code
#include <stdio.h>
int main() {
int var = 9; /* actual variable declaration */
printf("Address of var variable: %x\n", var);
return 0;
}
if var is 1 - 9 it prints 1 - 9. no problems
if var is 10 - 15 it prints 1 - f. Doh
This appears to be treating an int as a hexadecimal value. why is it doing this.
The specefier x or X is for Unsigned hexadecimal integer and not for addresses (pointers). for addresses you need to use the p character specefier.
if you want the address of the var . use p which is the conversion specifier to print pointers. see below: e.g.
int var = 9;
printf("%p\n", (void *) &var);
it seams "%x" in the string makes it print the value as a hexidecimal. I needed to use "%d"
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
i wrote code, and it gives me answers like 0.00 and 1.00, not actual math answer. Where i made mistake? (im begginer, dont scream on me :) )
#include <stdio.h>
#include <stdlib.h>
int main()
{
int a,b;
float x;// score
// Request 1
printf("Zadanie 1(12)\n");
// Calculate the square root of the given interval
printf("Oblicz pierwiastek rownania w podanym przedziale\n");
// Give me a number a
printf("Podaj liczbe a:\n");
scanf("%d",&a);
// Give me a number b
printf("Podaj liczbe b:\n");
scanf("%d", &b);
x=&a-&b;
//answer
printf("%f", x);
system("PAUSE");
return 0;
}
x = a - b
not
x = &a - &b
Explanation: The & operator gives you the memory address of a, which you need to give to scanf, so that it can place data there. But you do math on the actual value of a.. which is just a.
because of pointer arithmetic:
x = &a - &b;
computes the distance between the addresses of a and b, which are probably close since they're auto variables of the same type declared close-by. My guess is that you could get 1 or -1 (or any other integer value but not 0 since a and b are located in 2 separate addresses) then put in a float.
(the difference of addresses of 2 consecutive integers is 1, independently of the size of the integer)
You need of course to do:
x = a - b;
You've been misled by the fact that scanf needs a pointer on a or b to be able to write a value when reading the input.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
int main()
{
// Initialize & Declare variable
int m = 5;
// Allocates memory for storage of an integer variable
int *itemp;
// Stores memory address of variable m in memory address itemp
itemp = &m;
// Notice after declaring pointer you don't need to reference it as a pointer
// asterick is also known as indirection operator
// indirect reference: Accessing the contents of a memory cell through a pointer variable that stores it's address
// We can rewrite the contents in the memory cell as such
*itemp = 35;
printf("%d",*itemp);
// Doubles the value of m
*itemp = 2 * *itemp;
printf("%d",*itemp);
return 0;
}
It's returning 3570 instead of 70, which is what the book says it should be returning. What am I doing wrong?
The program is correct. It is printing what it is coded to print.
To clarify,
You have two printf()s, printing 35 and 70.
You don't have a "seperator" [for example, a newline (\n)] in your printf()s to distinguish the outputs of two print statements.
Result: You're seeing the final output 3570 as the combination of the output from two print statements, 35 and 70.
Solution: Add a \n or \t at the end of the format string supplied in printf() to add a visual seperator after each printf() to avoid confusion.
Just do
printf("%d\n",*itemp);
You are seeing 35 and 70 as output in the same line either add a space between them or a newline.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
#include <stdio.h>
int main() {
int a = 3;
float b = 6.412355;
printf("%.*f\n",a,b);
return 0;
}
Why the output is;
6.412
What is the effect of .* here ?
The . means that the next characters indicate the precision to use. The * means to read the value from the argument list; in your case, it will read a. The value is 3, so the next argument is printed to 3 decimal places.
In printf function, the format %[flags][width][.precision][length]specifier of this question is .precision, it has two choices number or *.
When *, it means The precision is not specified in the format string, but as an additional integer value argument preceding the argument that has to be formatted.
For more details, see http://www.cplusplus.com/reference/cstdio/printf/
#include <stdio.h>
int main() {
int a = 3;
float b = 6.412355;
printf("%.*f\n",a,b);
return 0;
}
It substiutes the value of a to the *,implying a precision.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Let us say that the English alphabets A to Z has value start from 1. A = 1, B = 2, C = 3 and so on. Write a program which calls a function which accepts the name of a person which is constant character array and returns an integer value with sum of the Alphapets. What is the benefit of passing the name of the person as const char array?
Suppose somebody else provides me a function that takes non-const char * and does the job. What the function is actually implemented is like this:
int get_int_sum(char *name)
{
int sum;
//codes to calculate sum of alphas
name[0] += 1;
//continue
return sum;
}
When I call the function using
char my_name[] = "Yu Hao";
int reuslt = get_int_sum(my_name);
Even if I got the result I want, my_name is changed to "Zu Hao" without my notice. However, if a function has a prototype of
int get_int_sum(const char*name)
I am sure that the string I passed will not be modified.
One advantage is that elements in a array are protected from changing its value.
For example, here is simple code.
int Your_function(const char * a)
{
a[3] = 'A'; // this statement causes compile error.
// do something
return 0;
}