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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:32:21+00:00 2026-05-11T09:32:21+00:00

I have some code that stack dumps when using sprintf to copy a a

  • 0

I have some code that stack dumps when using sprintf to copy a a pointer to strings. I am trying to copy the contents of animals into a new pointer array called output. However, I get a stack dump.

What should be in the output is the following: new animal rabbit new animal horse new animal donkey

An I going about this the right way?

Many thank,s

#include <stdio.h> #include <string.h> #include <stdlib.h>  void p_init(const char **animals, char **output);  int main(int argc, char **argv) {     char *animals[] = {'rabbit', 'horse', 'donkey', '\0'};       char **prt_animals = animals;     char *output[sizeof(*animals)];          /* print the contents here */     while(*prt_animals)     {         printf('Animal: %s\n', *prt_animals++);     }          /* copy and update in the output buffer */     p_init(*&animals, *&output);      getchar();      return 0;   void p_init(const char **animals, char **output) {     while(*animals)     {         sprintf(*output, 'new animal %s', *animals);          *output++;     } } 
  • 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-11T09:32:22+00:00Added an answer on May 11, 2026 at 9:32 am

    The array animals is an array of pointers. It is not an array of buffers of some size. Therefor, if you do

    sizeof(*animals) 

    You will get the sizeof of the first element of that array. Equivalent to

    sizeof(char*) 

    Because your array stores pointers. So, in the line that reads

    char *output[sizeof(*animals)]; 

    You allocate 4 or 8 pointers in one array (depends on how wide a pointer on your platform is. Usually it’s either 4 or 8). But that’s of course not senseful! What you wanted to do is create an array of pointers of the same size as animals. You will have to first get the total size of the animals array, and then divide by the size of one element

    char *output[sizeof(animals)/sizeof(*animals)]; 

    Now, that is what you want. But the pointers will yet have indeterminate values… Next you pass the array using *&animals (same for the other). Why that? You can pass animals directly. Taking its address and then dereference is the same as doing nothing in the first place.

    Then in the function you call, you copy the strings pointed to by elements in animal to some indeterminate destination (remember the elements of the output array – the pointers – have yet indeterminate values. We have not assigned them yet!). You first have to allocate the right amount of memory and make the elements point to that.

    while(*animals) {         // now after this line, the pointer points to something sensible         *output = malloc(sizeof('new animal ') + strlen(*animals));         sprintf(*output, 'new animal %s', *animals);          output++; // no need to dereference the result         animals++; // don't forget to increment animals too! } 

    Addition, about the sizeof above

    There’s one important thing you have to be sure about. It’s the way we calculate the size. Whatever you do, make sure you always have enough room for your string! A C string consists of characters and a terminating null character, which marks the end of the string. So, *output should point to a buffer that is at least as large so that it contains space for 'new animal ' and *animals. The first contains 11 characters. The second depends on what we actually copy over – its length is what strlen returns. So, in total we need

    12 + strlen(*animals) 

    space for all characters including the terminating null. Now it’s not good style to hardcode that number into your code. The prefix could change and you could forget to update the number or miscount about one or two characters. That is why we use sizeof, which we provide with the string literal we want to have prepended. Recall that a sizeof expression evaluates to the size of its operand. You use it in main to get the total size of your array before. Now you use it for the string literal. All string literals are arrays of characters. string literals consist of the characters you type in addition to the null character. So, the following condition holds, because strlen counts the length of a C string, and does not include the terminating null character to its length

    // 'abc' would have the type char[4] (array of 4 characters) sizeof '...' == strlen('...') + 1 

    We don’t have to divide by the size of one element, because the sizeof char is one anyway, so it won’t make a difference. Why do we use sizeof instead of strlen? Because it already accounts for the terminating null character, and it evaluates at compile time. The compiler can literally substitute the size that the sizeof expression returns.

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

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • 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 Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

First, the problem: I have several free projects, and as any software they contains
We have a library that deals with many aspects of error reporting. I have
I'm fairly new to HTTPS/SSL/TLS and I'm a bit confused over what exactly the
I have a navigation controller based application and I'm running into a strange issue

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.