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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T22:24:12+00:00 2026-05-16T22:24:12+00:00

I practiced an array of strings with no initial values. Attempt 1 #include <stdio.h>

  • 0

I practiced an array of strings with no initial values.

Attempt 1

#include <stdio.h>

char *array[] = {};
int main(int argc, char *argv[]) {
    array[0]="Hello";
    array[1]="World";

    char **i = array;
    while (*i) {
        printf("%d %s\n", i, *i);
        i++;
    }
}

$ gcc array_of_strings.c && ./a.out

6293704 Hello
6293712 World

It works fine.

Attempt 2

I thought I could move array pointer inside main function.

#include <stdio.h>

int main(int argc, char *argv[]) {
    char *array[] = {};
    array[0]="Hello";
    array[1]="World";

    char **i = array;
    while (*i) {
        printf("%d %s\n", i, *i);
        i++;
    }
}

$ gcc array_of_strings.c && ./a.out

-1899140568 (j͎?
-1899140560 World
-1899140552 ???%Y
-1899140544 1?I??^H??H???PTI???@
-1899140536 d?͎?
Segmentation fault

Huh, why it is not working? It causes “Segmentation fault” with ugly output.
Could somebody explain why I should not do this way?

  • 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-16T22:24:13+00:00Added an answer on May 16, 2026 at 10:24 pm

    Two problems.

    1. You aren’t allocating space for the array items. With your empty initializer list you are allocating an empty array. When you write to array[0] and array[1] you are writing to memory you do not own.

    2. You are getting lucky when you allocate the array globally. Global (aka statically-allocated) memory blocks tend to be filled with zeros. This is good for you because your while loop depends on their being a NULL pointer at the end of the array.

      When you allocate on the stack and access memory past the end of the array you will get whatever happens to already be on the stack, which can be any arbitrary garbage. Your while (*i) loop doesn’t get the NULL pointer it expects so it will keep on reading the garbage data until it finds some zeros that look like a NULL pointer.

    To fix #1, give an explicit length to the array. To fix #2, you must explicitly add a NULL pointer to the end of the array.

    char *array[3];
    array[0]="Hello";
    array[1]="World";
    array[2]=NULL;
    

    Also, for what it’s worth, pointers aren’t guaranteed to be the same size as ints. It is better to use %p to print pointers rather than %d.

    printf("%p %s\n", i, *i);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With equals sign: object HelloWorld { def main(args: Array[String]) = { println(Hello!) } }
Best practice when converting DataColumn values to an array of strings? [Edit] All values
I have an array of strings and I wish to find out if that
I have two array with string values, and one dictionary, im trying to add
I am trying to learn Java now and this is the hello world program
Which is the best practice in this situation? I would like an un-initialized array
Could someone show me a practical a use of the ... array method delcaration?
I'm still not practiced in oop.. now I know the importantness of it :)
What are some of the more practiced methods of speeding up jquery animations across
I need to save a string array to the database but it won't let

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.