#include <stdio.h>
/* Swaps the values of two integers */
void swap(int *var1, int *var2){
int tmp = *var1;
*var1 = *var2;
*var2 = tmp;
}
int main(){
int var1=-1,var2=-1;
while (var1 == -1 || var2 == -1) {
printf("Enter two integers: ");
if (scanf("%d %d", &var1, &var2) != 2) {
printf("Invalid input, please try again.\n");
while (getchar() != '\n'); // clear input buffer
var1 = var2 = -1; // reset variables
}
}
/* Swap the values of the two integers */
swap(&var1, &var2);
/* Print the swapped values */
printf("%d %d\n", var1, var2);
return 0;
}
or
#include <stdio.h>
/* Swaps the values of two integers */
int swap(int *var1, int *var2){
if (var1 == NULL || var2 == NULL) {
return -1; // indicate error
}
int tmp = *var1;
*var1 = *var2;
*var2 = tmp;
return 0; // indicate success
}
int main(){
int var1, var2;
/* Get two integer inputs from user */
while (scanf("%d %d", &var1, &var2) != 2) {
printf("Invalid input, please try again: ");
while (getchar() != '\n'); // clear input buffer
}
/* Swap the values of the two integers */
int result = swap(&var1, &var2);
/* Check for errors */
if (result != 0) {
printf("Error: invalid input\n");
return 0;
}
/* Print the swapped values */
printf("%d %d\n", var1, var2);
return 0;
}
#include <stdio.h>
#include <stdio.h>
char exchange_value(char *number1);// external function declaration // perameters
int main() {
char* value1="adarsh";//,value2,result;//here value2 variable becomes a pointer
//or size_t string_size = 16;
//char *string1 = (char*)malloc(string_size * sizeof(char));
//https://[Log in to view URL]
//int a=2,b=6;
//int *a=&a,*b=&b; we cannot pass arguments like this or actual perameters
exchange_value(value1); // function call arguments or calling function
printf("%s",value1);
}
char exchange_value(char *number1){ //function defination perameters or formal perameters
char *temperary_variable;
//temperary_variable=number1;
//number1=number2;
//number2=temperary_variable;
printf("%s",number1);
}
#if 0
int swap(int *a,int *b);
int main(){
int a,b,c;
printf("enter no's = ");
if(scanf("%d %d",&a,&b)!=2){
printf("no input\n");
return 0;
}
//scanf does not take "the address of operator (&)".
//It takes a pointer. Most often the pointer to the output variable
//is gotten by using the address-of operator in the scanf call,
//0
/*It's because you are storing something.Just think about how a function must work.
With printf, you can think of that as a void function that just outputs the
result and then it is done. With scanf you are wanting to RETAIN some data,
so you need a pointer aka address where the data you input will be stored
even after you leave the function. If you took an argument of data type,
say, "int", the data would be lost as soon as you exit the scanf function,
in other words, in the next line of code in the parent function,
that data you scanfed would be gone
if *a=*b what happens
No, it's not possible.
For every variable, machine allocates different addresses,but you want to know why?
Let's say, you have given value 4 for variable x , machine would allocate
some address like 10001 for the variable x, and when you call for the variable
again later anywhere, it will be able to find the value you are looking for by
the address it allocated previously, i mean it would load 10001 and give you
the value 4.
Let's assume your case, if you give 2 values like 4 and 5 for the same
address(even it's not possible) then if you call again, what machine do usually,
it would load the address for you right? It would load a address but what value
it will assign? Either 4 or 5 ? The system can't take decision on it's own right?
So it will get struck.
So not to happen all these, two variables can't have same addresses not in c,
anywhere, you can transfer a variable address to store in a variable,
later you will learn more about this topic in pointers in c language,
what actually happens when we create a pointer
int *ptr --> 0x973 an adreess is been created
*ptr=&a --> 0x87 << from variable a is given wise value is 7
0x973 addresses 0x87
consider *a=0x87
*ptr=*a 0x973 = 0x87 crash the code 2 addresses cannot be same
*/
swap(&a,&b);
printf("The swaped values are : %d %d",a,b);
}
#include <stdio.h>
int ad(int *p){
int b=6;
*p=*p+b;
}
int main() {
int a=5;
int *p=&a;
ad(p);
printf("%d",*p);
}
int swap(int *a,int *b){
int temp;
temp=*a;// here we strore the address into the variable temp
*a=*b; //so if incase we use *temp then
*b=temp;//it takes the address and processes it into value
}
// char swap
#include <stdio.h>
#include <string.h>
#if 0
void swap(char *a,char *b){// the swaped values will not saved
char *temp=a; //address 0x643 and hello as value<- hello address 0x976
a=b;
b=temp;// hello value will be passes
printf("%s,%s",a,b);//swapping takes place here
}
int main() {
char *a="hello";
char *b="hi";
swap(a,b);
//printf("%s %s",a,b);//but not reflexted here
}
#endif
/*So the above swap() function doesn’t swap strings.
The function just changes local pointer variables and the changes are
not reflected outside the function. Let us see the correct ways for
swapping strings: */
#include <stdint.h>
void swap(uint8_t **a,uint8_t **b){
char *temp=*a;
*a=*b;
*b=temp;
}
int main() {
uint8_t *a="hello";
uint8_t *b="hi";
swap(&a,&b);
printf("%s %s",a,b);
}
/*If you are using character pointer for strings (not arrays) then
change str1 and str2 to point each other’s data. i.e., swap pointers.
In a function, if we want to change a pointer (and obviously we want
changes to be reflected outside the function) then we need to pass a pointer
to the pointer.
This method cannot be applied if strings are stored using character arrays.
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/* Swaps strings by swapping data*/
void swap2(char *str1, char *str2)
{
char *temp = (char *)malloc((strlen(str1) + 1) * sizeof(char));
strcpy(temp, str1);
strcpy(str1, str2);
strcpy(str2, temp);
free(temp);
}
int main()
{
char str1[10] = "geeks";//at the starting only we use *str then after str the value
char str2[10] = "forgeeks";
swap2(str1, str2);
printf("str1 is %s, str2 is %s", str1, str2);
getchar();
return 0;
}
/*This method cannot be applied for strings stored in read-only block of memory.
Time Complexity: O(N)
Auxiliary Space: O(N)
------------------------------------------------------------------------------------
Also notice that we have used the code name instead of &name with scanf().
scanf("%s", name);
This is because name is a char array, and we know that array names
decay to pointers in C.
Thus, the name in scanf() already points to the address of the
first element in the string, which is why we don't need to use &.
The conversion of arr from an array type to a pointer type is known
as array decaying, i.e. the array has “decayed” to a pointer.
The two parameter definitions int* arr and int arr[] are actually the same.
Perhaps this is where the confusion comes from.
// a[2] <=> *(a + 2)
*/
To embed this program on your website, copy the following code and paste it into your website's HTML: