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 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

Ask A Question

Stats

  • Questions 124k
  • Answers 124k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Are you running on OS 3.0? I saw the same… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer It looks like you need to register Apache::Session::Memcached with Apache::Session::Wrapper,… May 12, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Use DATENAME or DATEPART: SELECT DATENAME(dw,GETDATE()) -- Friday SELECT DATEPART(dw,GETDATE())… May 12, 2026 at 1:19 am

Related Questions

I have a bit of code I'm running to test multithreading in MATLAB mex
I have created a dynamic typing system in C in order to create a
I am working on a project where I need to create a boundary around
I'm working a .NET web service that will be processing a text file with

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.