Results 1 to 8 of 8

Thread: Need help with pointers and arrays

  1. #1
    Join Date
    Dec 2007
    Beans
    49

    Need help with pointers and arrays

    Hello

    I have just started learning C and I am having some issues with some code in a program I am creating.

    Here is a shortened example of what I want to do but I get a "Segmentation fault".

    So I want to assign a string "O"(not zero) to each array in board1, essentially creating a 5x5 grid of "O".

    If some one can explain briefly what I need to do to get this to work, it would be very much appreciated.
    Do I even need to use pointers?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int i, j;
    char *board1[5][5];
    int main()
    {
    for(i = 0; i < 5; i++)
        {
        for(j = 0; j < 5; j++)
         {
         strcpy(board1[i][j], "O");
         printf("%s ", board1[i][j]);
         }
        printf("\n");
        }
    }
    Last edited by 3djake; August 1st, 2015 at 03:30 AM.

  2. #2
    Join Date
    Dec 2007
    Beans
    49

    Re: Need help with pointers and arrays

    Just an update

    So if I remove [j] from my program, I get a 5x5 grid of "O"'s. This is not ideal however because its just printing the same array 5 times. I want a 25 grid.

    This is my current solution which I am not too happy about, I am sure there is an easier and less time consuming way.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int i, j, f, g;
    char *board1[25];
    int main()
    {
      for(i = 0; i < 25; i++)
      {
        if ((board1[i] = malloc(sizeof(char))) == NULL)
         {
                printf("Memory or array Error\n");
                return -1;
         }
         if(i < 5)
         {
             strcpy(board1[i], "O");
             printf("%s ", board1[i]);
         }
         if(i == 4)
         {
            printf("\n");
         }
         if(i >= 5 && i < 10)
         {
            strcpy(board1[i], "O");
            printf("%s ", board1[i]);
         }
         if(i == 9)
         {
            printf("\n");
         }
         if(i >= 10 && i < 15)
         {
            strcpy(board1[i], "O");
            printf("%s ", board1[i]);
         }
         if(i == 14)
         {
            printf("\n");
         }
         if(i >= 15 && i < 20)
         {
            strcpy(board1[i], "O");
            printf("%s ", board1[i]);
         }
    
         if(i == 19)
         {
            printf("\n");
         }
         if(i >= 20 && i < 25)
         {
            strcpy(board1[i], "O");
            printf("%s ", board1[i]);
         }
    
         if(i == 24)
         {
            printf("\n");
         }
      }
    }
    Last edited by 3djake; August 1st, 2015 at 06:37 AM.

  3. #3
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Need help with pointers and arrays

    Actually, using a single-dimensional array is not that bad, as long as you manage the arithmetic (with respect to rows and columns) to calculate the offset into the array.

    You have not offered an explanation as to why you require to store a string, as opposed to a single character. If you follow up with a reply to this thread, then you should elaborate as to why you find it necessary to do so.

    Otherwise, here's a simple program that initializes an array to all O's, and then prints out the array in a presentable format:
    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        static int rows = 5;
        static int cols = 5;
    
        char board[rows * cols];   // no need to allocate on heap; size is small enough for stack
    
        // initialize board
        for (int r = 0; r < rows; ++r)
        {
            for (int c = 0; c < cols; ++c)
            {
                board[r * rows + c] = 'O';
            }
        }
    
        // verify board state
        for (int i = 0; i < rows * cols; ++i)
        {
            if (i > 0 && i % cols == 0)
            {
                printf("\n");
            }
    
            printf("%c ", board[i]);
        }
        printf("\n");
    
        return 0;
    }
    P.S. Avoid global variables!!!

  4. #4
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,588
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Need help with pointers and arrays

    Quote Originally Posted by dwhitney67 View Post
    Code:
                board[r * rows + c] = 'O';
    This is only "correct" because rows == cols. For a more general solution you would want:
    Code:
                board[r * cols + c] = 'O';
    Since it is so easy for even the most experienced to make this mistake, I think it better to use a 2 dimensional array and let the compiler do the math.

  5. #5
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Need help with pointers and arrays

    Quote Originally Posted by spjackson View Post
    This is only "correct" because rows == cols. For a more general solution you would want:
    Code:
                board[r * cols + c] = 'O';
    Since it is so easy for even the most experienced to make this mistake, I think it better to use a 2 dimensional array and let the compiler do the math.
    Thanks for the bug catch. I don't always bother to test the code I post on UF.

  6. #6
    Join Date
    Dec 2007
    Beans
    49

    Re: Need help with pointers and arrays

    Thanks for the feedback, I knew there was a more elegant way to do it but I only started learning C 3 weeks ago.

    Its for my Battleships game which you can have a look at here.
    https://github.com/3djake/Battleship.../Battleships.c

    I will try out some of your suggestions after the weekend, I have got some assignments for Uni to do.

    Cheers, Jake.

  7. #7
    Join Date
    Dec 2013
    Beans
    Hidden!

    Re: Need help with pointers and arrays

    The reason for the segmentation fault is that strcpy() copies strings, and strings include the terminating null character.



    strcpy

    char * strcpy ( char * destination, const char * source );

    Copy string
    Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

    To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.


    -
    -
    Last edited by xb12x2; August 1st, 2015 at 08:42 PM.

  8. #8
    Join Date
    Feb 2009
    Beans
    1,469

    Re: Need help with pointers and arrays

    Quote Originally Posted by xb12x2 View Post
    The reason for the segmentation fault is that strcpy() copies strings, and strings include the terminating null character.
    No, it's because the pointer passed to strcpy is uninitialized. This has the same problem despite omitting the terminator:
    Code:
    strncpy(board[i][j], "O", 1);

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •