Thread: How do I display char 2darray in string form?

  1. #1
    Registered User
    Join Date
    Apr 2005

    How do I display char 2darray in string form?

    The function like this
    #include <stdio.h>
    #include <stdlib.h>
    #define MAX_NAME 25
    int main()
    	char **name;
    	int i;
    	scanf("%d", &n);
    	name = malloc(n * sizeof *name);
    	for (i = 0; i < n; i++)
    		name[i] = malloc(MAX_NAME * sizeof *name);
    	for (i = 0; i < n; i++)
    		scanf("%s", &name[i]);
    	for (i = 0; i < n; i++)
    		printf("%s", name[i]);
    	return 0;
    This is display
    Segmentation fault
    I just wanna display the array of name. Where is the wrong? Can do this, char *name[25], instead of n column and 25 row?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    n is undeclared.

    I don't think this does what you want.
    scanf("%s", &name[i]);
    Get rid of the &.

    You should also free your memory when you're done with it.

    Can do this, char *name[25], instead of n column and 25 row?
    Why don't you try it and find out? Yes, you can, if you mean instead of char **name. [/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.

  3. #3
    Registered User
    Join Date
    Nov 2006
    name[i] = malloc(MAX_NAME * sizeof *name);
    * sizeof *name isn't what you're probably wanting to use here, your gonna get alot more memory than your expecting (probably 4 times more)

    just malloc(MAX_NAME); will suffice

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > name[ i ] = malloc(MAX_NAME * sizeof *name)
    name[ i ] = malloc(MAX_NAME * sizeof *name[ i ]);

    p = malloc ( n * sizeof *p );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 05:48 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM