Thread: Problem with an array of pointers

  1. #1
    Registered User
    Join Date
    Jul 2017
    Posts
    6

    Talking Problem with an array of pointers

    Code:
    #include <stdio.h>
    
    int main()
    {
        char *p[] = {"One","Two","Three"};
        int i=0;
        while(p[i] != NULL)
            printf("%s\n", p[i++]);
    
        return 0;
    }
    EOF and other constants do not help instead of NULL, it still produces a segmentation error. I need to make sure that regardless of the number of elements in the array, they are all displayed on the screen and then the while loop is turned off, and without any segmentation errors.
    Questions to answer the question is not necessary, I studied this topic but the answer was not found.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Try
    Code:
    char *p[] = {"One","Two","Three",NULL};
    > EOF and other constants do not help instead of NULL, it still produces a segmentation error.
    Well they would, because your test is != NULL to continue.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Super-Man View Post
    Questions to answer the question is not necessary, I studied this topic but the answer was not found.
    Gee, I wonder who this could possibly be....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with Pointers and array
    By platoali in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2011, 02:04 PM
  2. Array of pointers problem
    By Ryukugan in forum C++ Programming
    Replies: 1
    Last Post: 06-04-2009, 01:51 PM
  3. Problem with Pointers and an Array
    By thetinman in forum C Programming
    Replies: 1
    Last Post: 05-10-2007, 02:20 PM
  4. char array pointers problem
    By gsoft in forum C Programming
    Replies: 33
    Last Post: 04-01-2007, 01:52 AM
  5. array of pointers problem
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2006, 09:48 PM

Tags for this Thread