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












0
















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 '18 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 '18 at 18:08
















0
















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 '18 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 '18 at 18:08














0












0








0









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 '18 at 18:03









JakubJakub

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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 18:08












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?