Pointers in the functions does not return the proper values












0















So I have a code sample in Dev C++ in which I am trying to swap the values of s1 and s2 and print it out but somehow, the values doesn't change. I checked the function and it displays the proper values correctly, but when in the main, the values does not change.



 void swap_pointers(char *x,char *y){
char *tmp;
tmp = x;
x = y;
y = tmp;
printf("%sn",x);
printf("%snn",y);
}

int main()
{
char *s1, *s2;
s1 = "I should print second";
s2 = "I should print first";

swap_pointers(s1,s2);
printf("-AFTER SWAPPING-nn");
printf("s1 is %sn",s1);
printf("s2 is %sn",s2);

return 0;
}









share|improve this question























  • In C arguments are passed by value, meaning they are copied. Inside a function, all you have is the copies. Modifying a copy will not modify the original. To solve it I recommend that you do some research about emulating pass by reference in C.

    – Some programmer dude
    Nov 21 '18 at 8:47













  • Oh, is the code you're writing really C++? Or is it C? The only C++ specific bit is the lack of void as argument to the main function. If this is really C++, then the natural solution is to use std::swap instead of making your own. Or if you do it for educational reasons then read about references (which C doesn't have).

    – Some programmer dude
    Nov 21 '18 at 9:01











  • You have websites where I can start atleast so I can solve this further? I just want my program to work so I can try to learn from those mistakes.

    – Abraham
    Nov 21 '18 at 9:23













  • First of all, asking for links is not on topic. Secondly, we could not have provided proper links anyway since we don't know if you're programming in C or C++ (the dev-c++ tag is for the development environment, not the language). Lastly, use your favorite search engine, it should be very good at finding websites.

    – Some programmer dude
    Nov 21 '18 at 9:26













  • I don't know if Dev C++ has a syntax of its own but I can say I'm programming in C++ platform.

    – Abraham
    Nov 21 '18 at 9:28
















0















So I have a code sample in Dev C++ in which I am trying to swap the values of s1 and s2 and print it out but somehow, the values doesn't change. I checked the function and it displays the proper values correctly, but when in the main, the values does not change.



 void swap_pointers(char *x,char *y){
char *tmp;
tmp = x;
x = y;
y = tmp;
printf("%sn",x);
printf("%snn",y);
}

int main()
{
char *s1, *s2;
s1 = "I should print second";
s2 = "I should print first";

swap_pointers(s1,s2);
printf("-AFTER SWAPPING-nn");
printf("s1 is %sn",s1);
printf("s2 is %sn",s2);

return 0;
}









share|improve this question























  • In C arguments are passed by value, meaning they are copied. Inside a function, all you have is the copies. Modifying a copy will not modify the original. To solve it I recommend that you do some research about emulating pass by reference in C.

    – Some programmer dude
    Nov 21 '18 at 8:47













  • Oh, is the code you're writing really C++? Or is it C? The only C++ specific bit is the lack of void as argument to the main function. If this is really C++, then the natural solution is to use std::swap instead of making your own. Or if you do it for educational reasons then read about references (which C doesn't have).

    – Some programmer dude
    Nov 21 '18 at 9:01











  • You have websites where I can start atleast so I can solve this further? I just want my program to work so I can try to learn from those mistakes.

    – Abraham
    Nov 21 '18 at 9:23













  • First of all, asking for links is not on topic. Secondly, we could not have provided proper links anyway since we don't know if you're programming in C or C++ (the dev-c++ tag is for the development environment, not the language). Lastly, use your favorite search engine, it should be very good at finding websites.

    – Some programmer dude
    Nov 21 '18 at 9:26













  • I don't know if Dev C++ has a syntax of its own but I can say I'm programming in C++ platform.

    – Abraham
    Nov 21 '18 at 9:28














0












0








0








So I have a code sample in Dev C++ in which I am trying to swap the values of s1 and s2 and print it out but somehow, the values doesn't change. I checked the function and it displays the proper values correctly, but when in the main, the values does not change.



 void swap_pointers(char *x,char *y){
char *tmp;
tmp = x;
x = y;
y = tmp;
printf("%sn",x);
printf("%snn",y);
}

int main()
{
char *s1, *s2;
s1 = "I should print second";
s2 = "I should print first";

swap_pointers(s1,s2);
printf("-AFTER SWAPPING-nn");
printf("s1 is %sn",s1);
printf("s2 is %sn",s2);

return 0;
}









share|improve this question














So I have a code sample in Dev C++ in which I am trying to swap the values of s1 and s2 and print it out but somehow, the values doesn't change. I checked the function and it displays the proper values correctly, but when in the main, the values does not change.



 void swap_pointers(char *x,char *y){
char *tmp;
tmp = x;
x = y;
y = tmp;
printf("%sn",x);
printf("%snn",y);
}

int main()
{
char *s1, *s2;
s1 = "I should print second";
s2 = "I should print first";

swap_pointers(s1,s2);
printf("-AFTER SWAPPING-nn");
printf("s1 is %sn",s1);
printf("s2 is %sn",s2);

return 0;
}






function pointers dev-c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 21 '18 at 8:46









AbrahamAbraham

44




44













  • In C arguments are passed by value, meaning they are copied. Inside a function, all you have is the copies. Modifying a copy will not modify the original. To solve it I recommend that you do some research about emulating pass by reference in C.

    – Some programmer dude
    Nov 21 '18 at 8:47













  • Oh, is the code you're writing really C++? Or is it C? The only C++ specific bit is the lack of void as argument to the main function. If this is really C++, then the natural solution is to use std::swap instead of making your own. Or if you do it for educational reasons then read about references (which C doesn't have).

    – Some programmer dude
    Nov 21 '18 at 9:01











  • You have websites where I can start atleast so I can solve this further? I just want my program to work so I can try to learn from those mistakes.

    – Abraham
    Nov 21 '18 at 9:23













  • First of all, asking for links is not on topic. Secondly, we could not have provided proper links anyway since we don't know if you're programming in C or C++ (the dev-c++ tag is for the development environment, not the language). Lastly, use your favorite search engine, it should be very good at finding websites.

    – Some programmer dude
    Nov 21 '18 at 9:26













  • I don't know if Dev C++ has a syntax of its own but I can say I'm programming in C++ platform.

    – Abraham
    Nov 21 '18 at 9:28



















  • In C arguments are passed by value, meaning they are copied. Inside a function, all you have is the copies. Modifying a copy will not modify the original. To solve it I recommend that you do some research about emulating pass by reference in C.

    – Some programmer dude
    Nov 21 '18 at 8:47













  • Oh, is the code you're writing really C++? Or is it C? The only C++ specific bit is the lack of void as argument to the main function. If this is really C++, then the natural solution is to use std::swap instead of making your own. Or if you do it for educational reasons then read about references (which C doesn't have).

    – Some programmer dude
    Nov 21 '18 at 9:01











  • You have websites where I can start atleast so I can solve this further? I just want my program to work so I can try to learn from those mistakes.

    – Abraham
    Nov 21 '18 at 9:23













  • First of all, asking for links is not on topic. Secondly, we could not have provided proper links anyway since we don't know if you're programming in C or C++ (the dev-c++ tag is for the development environment, not the language). Lastly, use your favorite search engine, it should be very good at finding websites.

    – Some programmer dude
    Nov 21 '18 at 9:26













  • I don't know if Dev C++ has a syntax of its own but I can say I'm programming in C++ platform.

    – Abraham
    Nov 21 '18 at 9:28

















In C arguments are passed by value, meaning they are copied. Inside a function, all you have is the copies. Modifying a copy will not modify the original. To solve it I recommend that you do some research about emulating pass by reference in C.

– Some programmer dude
Nov 21 '18 at 8:47







In C arguments are passed by value, meaning they are copied. Inside a function, all you have is the copies. Modifying a copy will not modify the original. To solve it I recommend that you do some research about emulating pass by reference in C.

– Some programmer dude
Nov 21 '18 at 8:47















Oh, is the code you're writing really C++? Or is it C? The only C++ specific bit is the lack of void as argument to the main function. If this is really C++, then the natural solution is to use std::swap instead of making your own. Or if you do it for educational reasons then read about references (which C doesn't have).

– Some programmer dude
Nov 21 '18 at 9:01





Oh, is the code you're writing really C++? Or is it C? The only C++ specific bit is the lack of void as argument to the main function. If this is really C++, then the natural solution is to use std::swap instead of making your own. Or if you do it for educational reasons then read about references (which C doesn't have).

– Some programmer dude
Nov 21 '18 at 9:01













You have websites where I can start atleast so I can solve this further? I just want my program to work so I can try to learn from those mistakes.

– Abraham
Nov 21 '18 at 9:23







You have websites where I can start atleast so I can solve this further? I just want my program to work so I can try to learn from those mistakes.

– Abraham
Nov 21 '18 at 9:23















First of all, asking for links is not on topic. Secondly, we could not have provided proper links anyway since we don't know if you're programming in C or C++ (the dev-c++ tag is for the development environment, not the language). Lastly, use your favorite search engine, it should be very good at finding websites.

– Some programmer dude
Nov 21 '18 at 9:26







First of all, asking for links is not on topic. Secondly, we could not have provided proper links anyway since we don't know if you're programming in C or C++ (the dev-c++ tag is for the development environment, not the language). Lastly, use your favorite search engine, it should be very good at finding websites.

– Some programmer dude
Nov 21 '18 at 9:26















I don't know if Dev C++ has a syntax of its own but I can say I'm programming in C++ platform.

– Abraham
Nov 21 '18 at 9:28





I don't know if Dev C++ has a syntax of its own but I can say I'm programming in C++ platform.

– Abraham
Nov 21 '18 at 9:28












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408200%2fpointers-in-the-functions-does-not-return-the-proper-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408200%2fpointers-in-the-functions-does-not-return-the-proper-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)