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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:29:07+00:00 2026-05-26T14:29:07+00:00

I am (trying to) write a server-side daemon in c, and it accepts connections

  • 0

I am (trying to) write a server-side daemon in c, and it accepts connections from clients. I need a struct that keeps information on each open connection, so I have created an array of my defined struct, and I have it dynamically re-sizing with realloc.

The problem I have is creating the struct within the array. I keep getting this error:

test.c:41: error: conversion to non-scalar type requested

What am I doing wrong?

I spend most of my time in PHP, and am a noob with c. I realize that I am making some simple, beginner mistakes (in other words, feel free to make fun of me). If I am doing something stupid, please let me know. I’ve put my quality time in with google, but have not figured it out. I have reproduced the issue on smaller scale, as below:

here is my test.h:

typedef struct test_ test;

and here is my test.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "test.h"

//define the struct
struct test_ {
    int id;
    char *ip;
    int user;
    char *str;
};

//yes, the list needs to be global
test *test_list;


//
// add an item to the global list
//
int add(int id, char *ip, int size)
{
    //
    // increment size
    if(id>size) {
        size = id;
        //try to expand the list
        test *tmp = realloc(test_list,size);
        if(tmp) {
            //it worked; copy list back
            test_list = tmp;
        } else {
            //out of memory
            printf("could now expand list\n");
            exit(1);
        }
    }
    //
    // HERE IS THE TROUBLE CODE::
    test_list[id] = (struct test)malloc(sizeof(test)+(sizeof(int)*5)+strlen(ip)+1);
    test_list[id].id = id;
    test_list[id].ip = malloc(strlen(ip));
        strcpy(test_list[id].ip,ip);
    test_list[id].user = 0;
    test_list[id].str = NULL;
}

//
// main
//
int main(void)
{
    //initialize
    int size = 1;
    test_list = malloc(size*sizeof(test));
    //add 10 dummy items
    int i;
    for(i=0; i<10; i++) {
        size = add(i, "sample-ip-addr", size);
    }
    //that's it!
    return 0;
}
  • 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-05-26T14:29:07+00:00Added an answer on May 26, 2026 at 2:29 pm

    Try changing

    test *tmp = realloc(test_list,size);
    

    to

    test *tmp = realloc(test_list,size*sizeof(test));
    

    then delete

    test_list[id] = (struct test)malloc(sizeof(test)+(sizeof(int)*5)+strlen(ip)+1);
    

    When you allocate for test_list, there’s already space for each member of the struct allocated, so you don’t need to do it again. You just have to allocate for any pointers within the struct

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

Sidebar

Related Questions

I'm trying to write the server side of my android app that uses C2DM.
Developing server side code i finally got my eyes X-crossed trying to write -
I'm trying to write an insert statement for a SQL Server table that inserts
I am trying to write a server side code for sending push notifications for
I am trying to write a code that opens an FTP server on my
I'm running a java application that we distribute as a server-side system. I'm trying
I am trying to write a server side script (PHP) for generating an SVG
I'm trying to write an asynchronous server that queries a SQL Server database and
I'm trying to make a web app that will read in a server-side CSV
I'm trying to write simple proxy server for some purpose. In it I use

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.