Is this correct?

#include <stdio.h>
    #incluede <string.h>
    void getname(char  name[][50],int num)

    int main()
    {
        void getname(char name[][50])
        {
            for(int i=0;i<numofname;++)
            scanf("%[^\n]",name[i]);
        }

Recommended Answers

All 8 Replies

Is it correct? No. You can't nest one function inside another.

    #include <stdio.h>
    #incluede <string.h>
    void getname(char  name[][50],int num)

    int main()
    {
        char name[5][50];
        int numofname = 5;
        getname(name,numofname);
     }      

     void getname(char name[][50],int numofname)
     {
            for(int i=0;i<numofname;++)
            scanf("%[^\n]",name[i]);
     }

Also ... interesting spelling :)

#include <stdio.h>
/* #incluede <string.h> */ /* that's how I feel sometimes :) */
#include <string.h>


void getnames( char name[][50], int num ) ;
void getnames2( char name[][50], int num ) ;
void shownames( char name[][50], int num ) ;



int main()
{
    char name[5][50];

    int numofnames = 5;
    getnames( name, numofnames );
    shownames( name, numofnames );

    getnames2(name, numofnames );
    shownames( name, numofnames );

    return 0;
}      



void getnames( char names[][50], int numofnames )
{
    int i;
    char r;
    printf( "Note: leading spaces, when using "
            "'scanf' input, will be 'skipped' ...\n\n" );

    for( i = 0; i < numofnames; ++ i )
    {
        printf( "With NO tabs or spaces, "
                "enter a name %d: ", i+1 ) ;
        scanf( "%49s", names[i] );

        /* now could 'flush' stdin ... */
        while( scanf("%c", &r) && (r != '\n') ) ; /* 'flush' ... */

    }
}

void getnames2( char names[][50], int numofnames )
{
    int i;
    char* p;
    printf( "Note: leading spaces, when using "
            "'fgets' input, will NOT be 'skipped' ...\n\n" );

    for( i = 0; i < numofnames; ++ i )
    {
        printf( "Tabs & spaces ok here, "
                "enter a name %d: ", i+1 ) ;
        fgets( names[i], 50, stdin );

        /* fix up fgets ... */
        p = strchr( names[i], '\n' );
        if( *p ) /* if( *p = 0 ) */
            *p = 0; /* overwrite '\n' char at end ... */
        else
            while( getchar() != '\n' ) ; /* 'flush' ... */

    }
}

void shownames( char names[][50], int numofnames ) 
{
    int i;
    for( i = 0; i < numofnames; ++ i )
        printf( "%s\n", names[i] );
}

Is this correct?

No, Not in the way you are doing it. There are several stuff missing in your code. Like in your format used in the scanf. Moreso, you might want to use fgets instead.

@Ancient Dragon:

You can't nest one function inside another.

I think it is possible to do that, only that, the nested function is only been seen within the function in which they are defined.

Like so,this works for me:

#include <stdio.h>
#include <string.h>

int main()
{
  char name[5][50];
  int numofname = 5;
  int i;

void getname(char name[][50],int numofname)
{
   for(i = 0; i < numofname; ++i)
      scanf("%s[^\n]",name[i]);
}

 for( i = 0;i < 5; i++)
   getname(name,numofname);

 for( i = 0;i < numofname; i++)
    printf("%d : %s\n", i, name[i]);

  return 0;
}      

But often times, this is not want one will want to use.

Like so,this works for me:

You need a different compiler.

I think it is possible to do that

You can not have one function inside another function in either C or C++ languages. You can declare a function prototype inside a function, but the function itself has to be coded outside any other function.

#include <stdio.h>
#include <string.h>

int main()
{
  char name[5][50];
  int numofname = 5;
  int i;

void getname(char name[][50],int numofname); // this is ok

 for( i = 0;i < 5; i++)
   getname(name,numofname);

 for( i = 0;i < numofname; i++)
    printf("%d : %s\n", i, name[i]);

  return 0;
}      

I think it is possible to do that, only that, the nested function is only been seen within the function in which they are defined.

Not in standard C. If nested functions works for you then you're relying on a compiler extension.

You can not have one function inside another function in either C or C++ languages. You can declare a function prototype inside a function, but the function itself has to be coded outside any other function.

I think that, the confusion here, may be that, in C++ classes, there are many examples of nested classes.

And there, in these nested classes, the functions can also be 'inline' ...

So ... '2teez' ... may be being confused ... by this?

Also ... the '2teez' may be aware that some programming languages DO permit 'nesting' functions .... and so ... may be confused because of that?

What other languages support is irrelevant, this is C. While I can understand the confusion, that doesn't change the importance of making it clear what's standard C and what's not.

Hi David W,

So ... '2teez' ... may be being confused ... by this?

Also ... the '2teez' may be aware that some programming languages DO permit 'nesting' functions .... and so ... may be confused because of that?

Confused? Far from it. While, I apperciate deceptikon pointing out why it was possible for the gcc compiler that I use, confusion is not part of the tags here please.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.