I am having trouble with return value of char * function in C [duplicate]

Multi tool use
Multi tool use











up vote
0
down vote

favorite













This question already has an answer here:




  • Returning Local Variable Pointers - C [duplicate]

    1 answer



  • Char array corrupted after its pointer is passed between functions [duplicate]

    2 answers



  • Capitalize string and return local variable

    3 answers




This function should read the first line from the text file and return it.



char *meno(FILE *fin) {
char meno1[50];
fscanf(fin," %[^n]",meno1 );
return meno1;
}


If there is a text file in the directory the main function opens it and executes the function above and should print it's return value(in my case four names) but it doesn't work and prints "ÚJ8" 4 times.



int main() {
FILE *fin;
char s[10], *x=NULL;
int i = 1;
while (1) {
sprintf(s, "text%d.txt", i);
i++;
fin = fopen(s, "r");
if (fin == NULL) {
break;
}
*meno(fin);
printf("%s n", meno ); // this doesn't work

}

}


I am a beginner.










share|improve this question













marked as duplicate by Paul R c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    You're trying to return a pointer to something that no longer exists when the function returns. You need to pick up an introductory book on C, learn some basics, and you'll understand why.
    – Carey Gregory
    Nov 20 at 18:08















up vote
0
down vote

favorite













This question already has an answer here:




  • Returning Local Variable Pointers - C [duplicate]

    1 answer



  • Char array corrupted after its pointer is passed between functions [duplicate]

    2 answers



  • Capitalize string and return local variable

    3 answers




This function should read the first line from the text file and return it.



char *meno(FILE *fin) {
char meno1[50];
fscanf(fin," %[^n]",meno1 );
return meno1;
}


If there is a text file in the directory the main function opens it and executes the function above and should print it's return value(in my case four names) but it doesn't work and prints "ÚJ8" 4 times.



int main() {
FILE *fin;
char s[10], *x=NULL;
int i = 1;
while (1) {
sprintf(s, "text%d.txt", i);
i++;
fin = fopen(s, "r");
if (fin == NULL) {
break;
}
*meno(fin);
printf("%s n", meno ); // this doesn't work

}

}


I am a beginner.










share|improve this question













marked as duplicate by Paul R c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.











  • 2




    You're trying to return a pointer to something that no longer exists when the function returns. You need to pick up an introductory book on C, learn some basics, and you'll understand why.
    – Carey Gregory
    Nov 20 at 18:08













up vote
0
down vote

favorite









up vote
0
down vote

favorite












This question already has an answer here:




  • Returning Local Variable Pointers - C [duplicate]

    1 answer



  • Char array corrupted after its pointer is passed between functions [duplicate]

    2 answers



  • Capitalize string and return local variable

    3 answers




This function should read the first line from the text file and return it.



char *meno(FILE *fin) {
char meno1[50];
fscanf(fin," %[^n]",meno1 );
return meno1;
}


If there is a text file in the directory the main function opens it and executes the function above and should print it's return value(in my case four names) but it doesn't work and prints "ÚJ8" 4 times.



int main() {
FILE *fin;
char s[10], *x=NULL;
int i = 1;
while (1) {
sprintf(s, "text%d.txt", i);
i++;
fin = fopen(s, "r");
if (fin == NULL) {
break;
}
*meno(fin);
printf("%s n", meno ); // this doesn't work

}

}


I am a beginner.










share|improve this question














This question already has an answer here:




  • Returning Local Variable Pointers - C [duplicate]

    1 answer



  • Char array corrupted after its pointer is passed between functions [duplicate]

    2 answers



  • Capitalize string and return local variable

    3 answers




This function should read the first line from the text file and return it.



char *meno(FILE *fin) {
char meno1[50];
fscanf(fin," %[^n]",meno1 );
return meno1;
}


If there is a text file in the directory the main function opens it and executes the function above and should print it's return value(in my case four names) but it doesn't work and prints "ÚJ8" 4 times.



int main() {
FILE *fin;
char s[10], *x=NULL;
int i = 1;
while (1) {
sprintf(s, "text%d.txt", i);
i++;
fin = fopen(s, "r");
if (fin == NULL) {
break;
}
*meno(fin);
printf("%s n", meno ); // this doesn't work

}

}


I am a beginner.





This question already has an answer here:




  • Returning Local Variable Pointers - C [duplicate]

    1 answer



  • Char array corrupted after its pointer is passed between functions [duplicate]

    2 answers



  • Capitalize string and return local variable

    3 answers








c






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 at 18:03









Jakub

1




1




marked as duplicate by Paul R c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Paul R c
Users with the  c badge can single-handedly close c questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 20 at 18:08


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 2




    You're trying to return a pointer to something that no longer exists when the function returns. You need to pick up an introductory book on C, learn some basics, and you'll understand why.
    – Carey Gregory
    Nov 20 at 18:08














  • 2




    You're trying to return a pointer to something that no longer exists when the function returns. You need to pick up an introductory book on C, learn some basics, and you'll understand why.
    – Carey Gregory
    Nov 20 at 18:08








2




2




You're trying to return a pointer to something that no longer exists when the function returns. You need to pick up an introductory book on C, learn some basics, and you'll understand why.
– Carey Gregory
Nov 20 at 18:08




You're trying to return a pointer to something that no longer exists when the function returns. You need to pick up an introductory book on C, learn some basics, and you'll understand why.
– Carey Gregory
Nov 20 at 18:08

















active

oldest

votes






















active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes

0HfXY9F42f0yn5AX5kwoe,dqGYR5c9o054DjWZp SHG,mH,EhuYx dh UzRz6,hvcU
yZlyhbR55fmKdAzRaTZFPC1Ck wBqz5 9jpz QJyrcn

Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to resolve this name issue having white space while installing the android Studio.?

C# WPF - Problem with Material Design Textbox