Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9000073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:15:52+00:00 2026-06-16T00:15:52+00:00

This is my code: char *genWord(int wordLen){ char array[wordLen + 1]; char *word; int

  • 0

This is my code:

char *genWord(int wordLen){
    char array[wordLen + 1];
    char *word;
    int i;

    for(i = 0; i <= wordLen; i++){
        array[i] = 'a';
    }

    array[wordLen] = '\0';

    //Test1 printf
    printf("%s \n", array);

    word = array;

    //Test 2
    printf("%s \n", word);

    return word;
}

main(){
    char *word;
    int wordLen = 10;

    word = (char *)genWord(wordLen);

    //Test 3
    printf("%s", word);       
}

And this is my output of 3 (same prog) executes in Linux gcc:

1st:

aaaaaaaaaaaaaaaaaaaaa 
aaaaaaaaaaaaaaaaaaaaa 
aaaaaaaaaaaaT��aaaaa

2nd:

aaaaaaaaaaaaaaaaaaaaa 
aaaaaaaaaaaaaaaaaaaaa 
aaaaaaaaaaaa�ƃ�aaaaa 

3rd:

aaaaaaaaaaaaaaaaaaaaa 
aaaaaaaaaaaaaaaaaaaaa 
aaaaaaaaaaaa����aaaaa   

Can’t figure out what’s wrong and even worse, can’t imagine how can i get a different output on every run? variable word can’t have garbage in a part of the string right?!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T00:15:52+00:00Added an answer on June 16, 2026 at 12:15 am

    You are returning local address, which is not anymore guaranteed to be allocated after the return from call. Therefore it is an undefined behaviour.

    When you do word = array the local address of the array is copied into word variable. After return the calling function has the address, which was the local address in the function. But after the function return the address is not anymore valid, and may be allocated for other purpose, or overwritten or anything.

    Do word = strdup (array); OR

    word = malloc (sizeof (char) * (strlen (array) + 1));
    strcpy (word, array);
    return word;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This code: int main(char[][] args) { MyObject obj; obj.x; return 0; } gives me:
I have this code: int main() { char ch[15]; cout<<strlen(ch)<<endl; //7 cout<<sizeof(ch)<<endl; //15 return
This code causes a Segmentation Fault: int main(){ char *p; char a[50] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa;
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
I have this code below: char buffer[10]; void main(int argc, char *argv[]) { strcpy(buffer,
why is the output fffffffa rather than 0000000a for this code char c=0Xaa; int
This is the code: int main() { char c = 0x00; //c |= 0x0A;
I'm wondering about this sample piece of code: int main() { char *p ;
Consider this code: const char* someFun() { // ... some stuff return Some text!!
I have this code struct Student { char name[48]; float grade; int marks[10,5]; char

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.