Help with dd array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Singleton

    #1

    Help with dd array

    what is wrong with my main fn?



    void f(char** p)

    {

    printf("%s", p[0]);

    }



    void main()

    {

    char a[50][50];

    strcpy(a[0], "hello");

    f(a);

    }


  • pemo

    #2
    Re: Help with dd array


    "Singleton" <kuchbhi@homeca ll.co.uk> wrote in message
    news:43874a5f$1 _4@mk-nntp-2.news.uk.tisca li.com...[color=blue]
    > what is wrong with my main fn?
    >
    >
    >
    > void f(char** p)
    >
    > {
    >
    > printf("%s", p[0]);
    >
    > }
    >
    >
    >
    > void main()
    >
    > {
    >
    > char a[50][50];
    >
    > strcpy(a[0], "hello");
    >
    > f(a);
    >
    > }
    >
    >[/color]

    Gosh, it's hard to know where to start ... but maybe you could say something
    about what you expect this to be doing?

    Something like this ...??

    #include <stdlib.h>

    void f(char * p)

    {
    printf("%c", p[0]);
    }



    int main(void)

    {
    char a[50][50];

    a[0][0] = 'e';

    f(&a[0][0]);
    }

    a is an array of 50 arrays of 50 characters. Is that what you thought it
    was?


    Comment

    • Richard Heathfield

      #3
      Re: Help with dd array

      Singleton said:
      [color=blue]
      > what is wrong with my main fn?
      >
      >
      >
      > void f(char** p)
      >
      > {
      >
      > printf("%s", p[0]);[/color]

      Undefined behaviour - calling a variadic function without a function
      prototype in scope. You forgot to #include <stdio.h>
      [color=blue]
      >
      > }
      >
      >
      >
      > void main()[/color]

      In C, main() returns int.
      [color=blue]
      >
      > {
      >
      > char a[50][50];
      >
      > strcpy(a[0], "hello");[/color]

      You'll want <string.h> as well, then.
      [color=blue]
      > f(a);[/color]

      f takes char **, but you're not passing a char **. In a value context, the
      name of an array decays into a pointer to its first element, so a has the
      type char (*)[50], which is not the same as char **.

      --
      Richard Heathfield
      "Usenet is a strange place" - dmr 29/7/1999

      email: rjh at above domain (but drop the www, obviously)

      Comment

      Working...