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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T12:19:36+00:00 2026-05-11T12:19:36+00:00

I have a structure that has an array of pointers. I would like to

  • 0

I have a structure that has an array of pointers. I would like to insert into the array digits in string format, i.e. ‘1’, ‘2’, etc..

However, is there any difference in using either sprintf or strncpy?

Any big mistakes with my code? I know I have to call free, I will do that in another part of my code.

Many thanks for any advice!

struct port_t {     char *collect_digits[100];  }ports[20];  /** store all the string digits in the array for the port number specified */ static void g_store_digit(char *digit, unsigned int port) {     static int marker = 0;     /* allocate memory */     ports[port].collect_digits[marker] = (char*) malloc(sizeof(digit)); /* sizeof includes 0 terminator */     // sprintf(ports[port].collect_digits[marker++], '%s', digit);     strncpy(ports[port].collect_digits[marker++], digit, sizeof(ports[port].collect_digits[marker])); } 
  • 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. 2026-05-11T12:19:37+00:00Added an answer on May 11, 2026 at 12:19 pm

    Yes, your code has a few issues.

    • In C, don’t cast the return value of malloc(). It’s not needed, and can hide errors.
    • You’re allocating space based on the size of a pointer, not the size of what you want to store.
    • The same for the copying.
    • It is unclear what the static marker does, and if the logic around it really is correct. Is port the slot that is going to be changed, or is it controlled by a static variable?

    Do you want to store only single digits per slot in the array, or multiple-digit numbers?

    Here’s how that function could look, given the declaration:

    /* Initialize the given port position to hold the given number, as a decimal string. */ static void g_store_digit(struct port_t *ports, unsigned int port, unsigned int number) {   char tmp[32];    snprintf(tmp, sizeof tmp, '%u', number);   ports[port].collect_digits = strdup(tmp); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data structure that represents C# code like this: class Namespace: string
I have an array that has a hierarchic structure. In this example there are
I have a database structure that has a Person table which contains fields such
I have a section of makefile that has this sort of structure: bob: ifdef
I have a tree data structure that is L levels deep each node has
I have a table in SQL server that has the normal tree structure of
Generally, MVC frameeworks have a structure that looks something like: /models /views /controllers /utils
I have a XML Structure that looks like this. <sales> <item name=Games sku=MIC28306200 iCat=28
I have a table structure that looks like: <table> <tr id=row1> <td> <div>row 1
I have a structure that contains an arrays of another structure, it looks something

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.