Thread: multi-dimensional arrays

  1. #1
    Registered User
    Join Date
    May 2008

    multi-dimensional arrays

    I am trying to learn multi-dimensional arrays in C/C++, but am not having much luck. I think I understand the way this type of array works, but I am having problems.
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_NAME_COUNT 10
    #define MAX_NAME_LEN 100
    int	main(){
    	char	*names[MAX_NAME_COUNT][MAX_NAME_LEN];
    	int	x;
    	printf("enter the names of 10 people:\n");
    	for(x = 1; x < MAX_NAME_COUNT; x++){
    		scanf("%s", names[x][0]);
    	printf("these are the names you entered are:\n");
    	for(x = 1; x < MAX_NAME_COUNT; x++){
    		printf("%s\n", names[x][0]);
    Does anyone know what is wrong in my code? There are no compilation errors, but the program crashes after the first name is entered.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    The array should not be an array of char*, but of just char. (And the scanf and printf should just be names[x], not names[x][0].)

  3. #3
    Registered User
    Join Date
    Apr 2008
    char	*names[MAX_NAME_COUNT][MAX_NAME_LEN];
    which declares a 2D array of pointers to char, should be:
    char	names[MAX_NAME_COUNT][MAX_NAME_LEN];
    which declare an array of (allocated) strings.

    scanf("&#37;s", names[x][0]);
    should be:
    as names[x][0] is only the 1st character of the xth string. You should also make sure scanf does not overrun the allocated space.

    printf("%s\n", names[x][0]);
    same thing here, this should be names[x] only.

    There are no compilation errors, but the program crashes after the first name is entered
    You were using unallocated memory.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Also note that you generally want for loops to start at 0 and count upwards, instead of at 1. array[0] is a valid element, after all, and if you start at array[1] you've wasted an entire element.

    [edit] 6,500th post! [/edit]

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Join Date
    Oct 2007
    Inside my computer

    Note that the last two are resources that should explain WHY your previous code won't work.
    Root4 is providing you with correct code (save for the scanf vulnerability).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 03:56 AM
  2. fscanf for multi dimensional arrays
    By rambos in forum C Programming
    Replies: 7
    Last Post: 05-06-2008, 03:26 AM
  3. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 04:49 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM