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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:40:01+00:00 2026-06-11T23:40:01+00:00

I was experimenting with ways to initialize arrays and strings in C, and found

  • 0

I was experimenting with ways to initialize arrays and strings in C, and found that:

char *str = "ABCDE";

perfectly initializes the string with no errors or warnings, but:

int *array = {1,2,3,4,5};

gives me warnings and eventually dumps core. It really bugs me now and I would like to know why this sort of declaration works for characters but doesn’t for integers…

EDIT: I’m using the gcccompiler.

  • 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-11T23:40:02+00:00Added an answer on June 11, 2026 at 11:40 pm

    It will work for ints by doing this:

    int array[] = {1,2,3,4,5};
    

    or this:

    int *array = (int[]){1,2,3,4,5};
    

    "string" tells the compiler all the information it needs (size,type) to instantiate the string (aka an array of bytes with a NULL terminator). A naked {} does not unless you declare it as a compound literal. Adding the ints[] tells the compiler that the initiated data is an array of ints.

    As Nathan pointed out in the comments there are subtle differences to the two statements.

    The first, defines an array of 5 ints on the stack. This array can be modified and lives until the end of the function.

    The second, 1) defines an anonymous array of five ints on the stack 2) defines a pointer ‘array’ to the first element of the anonymous array on the stack. The pointer should not be returned since the memory is on the stack. Also the array is not inherently const like a string literal.

    EDIT: Replaced cast with compound literal as pointed out by commentator.

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

Sidebar

Related Questions

Experimenting with Swing, I learnt that there are two ways of using JOptionPane and
I've been experimenting with creating a canvas element in a few different ways and
When experimenting with (embedded) Apache Derby DB, I noticed that a fresh database, with
I was experimenting with ways to get rid of some memory leaks within my
I'm experimenting with ways to draw a sinusoidal graph. My widget is only expecting
I have been experimenting with ways to read data from a SQL server as
I am experimenting with ways to implement a simplified Term Rewriting System (TRS)/Symbolic Algebra
We're experimenting with various ways to throttle user actions in a given time period
So... I'm making a Jeopardy game, and I'm experimenting with better ways of making
I'm experimenting with a few ways of doing this but just thought I'd ask

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.