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 7974547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:24:13+00:00 2026-06-04T08:24:13+00:00

I need an array of user structs. struct user { char *username; }; struct

  • 0

I need an array of user structs.

struct user {
    char *username;
};  

struct user users[10]; //Array of user structs

int main(int argc, char **args) {
    int initUsersArray();        
    char *username = "Max";
    addToUsersArrry(username);

}



int addToUsersArrry(username) {
int i;
i = 0;
struct user tmp;    
for(i;i<10;i++) {
    if(users[i] != NULL) 
        if(strcmp(*users[i].username,username)==0)
            return -1;
}
i = 0;
for(i;i<10;i++) {
    if(users[i] = NULL) { 
        users[i]=tmp;
        users[i].username=username;
        return 1;
    }   

}   

}

int initUsersArray() {  
int i;
i=0;
struct user tmp;

for(i;i<10;i++) {
    users[i] = (struct user*) calloc(1,sizeof(tmp));
}
}

My first question is, if it is the right way to init the users array with NULL like i did.
The second problem is, that
*users[i].username
and other parts of code where want to get/set the user at a specific position, dont work.
Regards

  • 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-04T08:24:16+00:00Added an answer on June 4, 2026 at 8:24 am

    Here, I fixed it for you. And don’t forget to diff and learn something. Don’t just c/p it.

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct user {
        char *username;
    } user;
    
    user *users; //Array of user structs
    
    int addToUsersArray(char *username) {
    int i = 0;
    
    for(; i<10; i++) {
        if(users[i].username=='\0') {
            users[i].username = username;
        return 1;
        } else if(strcmp(users[i].username, username) == 0)
        return -1;
    }
    return -1;
    }
    
    void initUsersArray() {  
        users = (user*) calloc(10, sizeof(user)); //10 of them
    }
    
    int main(int argc, char** argv) {
        initUsersArray();        
            char *username = "Max";
            addToUsersArray(username);
        username = "Ma1x";
        addToUsersArray(username);
        printf("%s\n",users[0].username);
        printf("%s\n",users[1].username); 
        return 1;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I have this string $str=word1 word2 word3; I need array( 'word1', 'word2', 'word3'
I need an array where size is known at compile time. I know I
I need an array to hold member-function-pointers of different classes. How can I define
I need an array of 820 zeros for using with a mathematical function. In
So I need an array from my data that I can foreach on the
Trying to split this string 主楼怎么走 into separate characters (I need an array) using
I need help regarding array insertion into mysql table. I have an array when
I am working with an array and need some help. I would like to
I have a problem with some numpy stuff. I need a numpy array to
I have string like {param1=foo}{param2=bar}hello world! I need to extract array of tuples (paramName,

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.