Initialize char **

Discussion in 'C Programming' started by rasika, Apr 5, 2004.

  1. rasika

    rasika Guest

    I have a structure
    typedef struct m
    {
    char *name,
    char **alais;
    }mystruct;

    The way I have initialised name field of the struct i want to intialise
    the alais field to { "geeta", NULL } char array .
    So how do i Do it?

    mystruct a={
    name:"myname",
    };
    rasika, Apr 5, 2004
    #1
    1. Advertising

  2. rasika wrote:
    > I have a structure
    > typedef struct m
    > {
    > char *name,
    > char **alais;
    > }mystruct;
    >
    > The way I have initialised name field of the struct i want to
    > intialise the alais field to { "geeta", NULL } char array .
    > So how do i Do it?

    Not sure I understand you, but any string in your code is null terminated,
    i.e.

    char *t="yourname";

    t actually points to a null terminated string, so you already have a null
    there.
    >
    > mystruct a={
    > name:"myname",
    > };
    Ahmed S. Badran, Apr 5, 2004
    #2
    1. Advertising

  3. "rasika" <> wrote in message news:...
    > I have a structure
    > typedef struct m
    > {
    > char *name,
    > char **alais;
    > }mystruct;
    >
    > The way I have initialised name field of the struct i want to intialise
    > the alais field to { "geeta", NULL } char array .
    > So how do i Do it?
    >
    > mystruct a={
    > name:"myname",
    > };


    F:\Vijay\C> type howto.c
    #include <stdio.h>
    #include <stdlib.h>

    typedef struct m
    {
    char *name;
    char **alais;
    }mystruct;

    mystruct a={
    .name= "myname",
    .alais= (char *[]) { "Geeta", "Seeta", NULL }
    /* I would want to know if this casting is correct! */
    };

    int
    main ( void )
    {
    int i = 0;

    printf ( "Name: %s\n", a.name );
    printf ( "Alais: \n" );
    while ( a.alais )
    printf ( "Name: %s\n", a.alais[i++] );
    return EXIT_SUCCESS;
    }

    F:\Vijay\C> gcc -std=c99 -Wall howto.c
    F:\Vijay\C> a.exe
    Name: myname
    Alais:
    Name: Geeta
    Name: Seeta

    --
    Vijay Kumar R Zanvar
    My Home Page - http://www.geocities.com/vijoeyz/
    Vijay Kumar R Zanvar, Apr 5, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. wwj
    Replies:
    7
    Views:
    542
  2. wwj
    Replies:
    24
    Views:
    2,484
    Mike Wahler
    Nov 7, 2003
  3. Ben Pfaff
    Replies:
    5
    Views:
    462
    Tristan Miller
    Jan 17, 2004
  4. Steffen Fiksdal

    void*, char*, unsigned char*, signed char*

    Steffen Fiksdal, May 8, 2005, in forum: C Programming
    Replies:
    1
    Views:
    571
    Jack Klein
    May 9, 2005
  5. lovecreatesbeauty
    Replies:
    1
    Views:
    1,015
    Ian Collins
    May 9, 2006
Loading...

Share This Page