multi-dimensional arrays

This is a discussion on multi-dimensional arrays within the C Programming forums, part of the General Programming Boards category; I am trying to learn multi-dimensional arrays in C/C++, but am not having much luck. I think I understand the ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    2

    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.
    Code:
    #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]);
    	}
    
    	return;
    }
    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
    Posts
    14,185
    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
    Posts
    395
    Code:
    char	*names[MAX_NAME_COUNT][MAX_NAME_LEN];
    which declares a 2D array of pointers to char, should be:
    Code:
    char	names[MAX_NAME_COUNT][MAX_NAME_LEN];
    which declare an array of (allocated) strings.

    Code:
    scanf("&#37;s", names[x][0]);
    should be:
    Code:
    scanf("%s",names[x]);
    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.

    Code:
    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
    Location
    Canada
    Posts
    8,046
    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]
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    http://cpwiki.sourceforge.net/Common..._the_right_way
    http://cpwiki.sourceforge.net/Common...llocating_them
    http://cpwiki.sourceforge.net/A_pointer_on_pointers

    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, 02: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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21