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

  • SEARCH
  • Home
  • 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 7770795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:29:44+00:00 2026-06-01T16:29:44+00:00

Hey so im struggling hard. I need to create a random string (of length

  • 0

Hey so im struggling hard. I need to create a random string (of length 2 to 6) and generate a random character for each one ‘A’ to ‘Z’. The problem is that I am trying to use a struct with a char* in it. then dynamically allocate each struct when i try to generate the random data.

struct TStruct 
{ 
int ID; 
float Value; 
int a[4]; 
char *Name; 
};

//create pointer to TSruct
typedef struct TStruct *ptrStruct;

//have ptrStruct point to 10 structs
ptrStruct structs[NUM_STRUCTS];

void genStruct(ptrStruct *alpha, int countID){
//declare variables
//ID counter
countID+=1;
int i;
int temp;
int tempChar;
int nameSize;
*alpha = (ptrStruct)malloc(sizeof(struct TStruct)); 
srand(time(0));
//put the ID in
(*alpha)->ID=countID;
//random number 0 to 999.99 
(*alpha)->Value= (float)rand()/((float)(RAND_MAX)+1000)/100;
//store 4 ints 0 to 100 into array a
for (i = 0; i < 4; i++) {
    //generate random number
    temp = rand() % 100;
    //put into the array
    (*alpha)->a[i] = temp;
}
//generate a random length for the name 2 to 6
nameSize = rand() % 4 + 2;
char buffer[2];
//run a for loop based on the size of nameSize
//THIS IS THE PROBLEM CODE!!!
for (i = 0; i < nameSize; i++) {
    snprintf(buffer,2, "%d",(rand() % 25)+65);
    strcat((*alpha)->Name,buffer);
}
}

Any help would be sooooo appreciated.
thanks

  • 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-01T16:29:45+00:00Added an answer on June 1, 2026 at 4:29 pm

    One primary problem is that you don’t allocate space for the string. You have an uninitialized char *Name in the structure.

    Frankly, for 6 (even 8) characters, you should simply allocate the array as part of the structure. On a 64-bit machine, the pointer would be bigger than the array; on a 32-bit machine, you’d still be using more space with a pointer plus data than with just the data.

    Your character generation loop is wonky too. The rand() expression is more or less reasonable (it won’t generate ‘Z’ and will be biassed towards the start of the alphabet), but you should use 'A' instead of 65, and you should simply assign the character to the relevant position in the name. Using snprintf() like that will get you the 10’s digit of the code for each letter, which is not what you want at all.

    Don’t forget to null terminate the string. And don’t forget that strcat() only works when the string is already null terminated; you haven’t fixed that, either.


    Joachim Pileborg made an accurate comment in a now deleted remark, that you should ensure you only call srand() once, usually at program startup. If you call the genStruct() function more than once in a second (and if you call it more than twice, you can pretty much guarantee that — even on an original IBM PC at 4 MHz — you will have at least two of the three calls in the same second) then you will get the same data generated each time srand() is called with the same number (time).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey, I'm really struggling with this one. I'am trying to port a small piece
Hey, I'm trying to create Like Buttons for Facebook Photos, but can't figure out
Hey guys I am struggling to create a query which gives schedule of a
Hey, I'm currently trying to extract information from a 3d array, where each entry
Hey all i have an XML file that i am trying to loop though.
Hey everyone. I'm really struggling to figure out the logic with this one and
Hey having some trouble trying to maintain transparency on a png when i create
Hey guys I have a weird problem trying to use a global array in
Hey I am new to PHp and I am trying to enter details into
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)

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.