What is output of following program? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
int main()
{
int i = 10;
int *p = &i;
foo(&p);
printf ("%d ", *p);
printf ("%d ", *p);
}
void foo(int **const p)
{
int j = 11;
*p = &j;
//Printing the vlue
printf("%d ", **p);
}

When foo returns, the pointer p in main points to a local variable that existed during the execution of foo. Since foo has ended, de-referencing that pointer invokes undefined behaviour. Therefore your program can output, or indeed do, anything.

Related

How to determine the number of appearances of a letter in a string using C language? [closed]

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 3 years ago.
Improve this question
This is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int appar(char c[], char x);
int main() {
char c[] = "hello everyone!";
int b = appar(c, 'h');
printf("nbr of h is %d ", b);
return 0;
}
int appar(char c[], char x) {
int i = 0, cmpt = 0;
int q = strlen(c);
for (i; i < q; i++) {
if (c[i] == 'x')
cmpt++;
}
return cmpt;
}
I run and compile the program, but I receive "nbr of h is 0".
What's the wrong in this code?
Change c[i]=='x' to c[i]==x
You want to compare with the variable x, not the character constant 'x'

how to pass pointers as parameters in a function? [closed]

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 3 years ago.
Improve this question
I am trying to print the sum and diffrence of numbers using pointers but i am getting only sum as output.
#include <stdio.h>
#include<math.h>
void update(int *a,int *b)
{
int sum,sub;
sum = *a + *b;
printf("",sum);
sub = abs(*a - *b);
printf("",sub);
}
int main()
{
int a, b;
int *pa = &a, *pb = &b;
scanf("%d %d", &a, &b);
update(pa, pb);
printf("%d\n%d", a, b);
return 0;
}
If I have understood correctly (taking into account the function name) you mean something like the following
#include <stdio.h>
#include <stdlib.h>
void update( int *a, int *b )
{
*a += *b;
*b = abs( *a - *b - *b );
}
int main(void)
{
int a, b;
scanf( "%d%d", &a, &b );
update( &a, &b );
printf( "a = %d, b = %d\n", a, b );
return 0;
}
If for example to enter two values 20 and 10 then the output will look like
a = 30, b = 10
Pay attention to that these calls of printf in your function
printf("",sum);
printf("",sub);
do not make sense.

Pointers to min and max values of a array [closed]

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 4 years ago.
Improve this question
I'm trying to write a program in which there are two pointers that point to the min and the max value of a 100-values array. Why does it give me error?
#include <stdio.h>
int main(void){
int array[100], i;
for(i=0; i<100; i++)
array[i]=i;
int *ptr1, *ptr2, flag=0;
ptr1 = &array[0];
ptr2 = &array[0];
while(!flag){
for(i=0;i<100;i++){
if(*ptr1 > array[i]){
ptr1 = &array(i);
break;
}else if(*ptr2 < array[i]){
ptr2 = &array(i);
break;
}
}
if(i==100)
flag=1;
}
printf("%d %d", *ptr1, *ptr2);
}
main.c:13:25: error: called object ‘array’ is not a function or function pointer
The statement
ptr1 = &array(i);
should be corrected to
ptr1 = &array[i];
So does the
ptr2 = &array(i);

Pointers to pointer usage giving unexpected result [duplicate]

This question already has answers here:
returning a local variable from function in C [duplicate]
(4 answers)
Closed 7 years ago.
Why is the First Printing Statement in main(), printing 11 ?
#include<stdio.h>
void foo(int ** p){
int j = 11;
*p = &j;
printf("%d ", **p); //Printing 11
}
int main(){
int i = 10;
int *p = &i;
foo(&p);
printf("%d ", *p); //Printing 11
printf("%d ", *p); //Printing Random value
return 0;
}
Inside foo() , you're assigning the address of a automatic local variable j to *p. After foo() has finished execution, j does not exist anymore and thus, using (derererencing) p furthermore in main() invokes undefined behavior.
Now, the output of UB is, well, undefined.

Swap two elements of an array function [closed]

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 5 years ago.
Improve this question
Can you help me by telling me what is wrong with this? Why is the swap function not working?
void swap(int a[], int b, int c) {
int temp = a[b];
a[b] = a[c];
a[b] = temp;
}
void bubble1 (int a[], int N){
int i;
for(i=0;i<N-1;i++){
if(a[i]>a[i+1]){
swap(a,i,i+1);
}
}
}
void main() {
int N = 11;
int a[12]={5,3,12,4,25,10,14,35,2,8,13};
bubble1 (a,N);
int i;
for(i = 0; i < N; i++){
printf("%d\n",a[i]);
}
}
If I don't use the swap function and do the swapping manually in the "bubble" function it works. However if I use the swap it doesn't work, even though it's exactly the same. What am I doing wrong here?
int temp = a[b];
a[b] = a[c];
a[b] = temp;
Simple typo, you are assigning to a[b] twice. The second one should be a[c]

Resources