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

struct name
{
  char a[20];
  double s;
};
void print(struct name v[][10],int row,int col)
{ 
    int i,j;
    for( i = 0; i < row; i++)
    {
        for(j = 0; j < col; j++){
            printf("%lf\n ",v[i][j].s );
        }
    }
}

int main()
{
  int m,n,i,j;
  scanf("%d %d",&m,&n);
  struct name v[m][n];
  for(i=0;i<m;i++)
    for(j=0;j<n;j++)
     scanf("%s %lf\n",v[i][j].a,&v[i][j].s);
  print(v,m,n);
   return 0;   
}

In the above program, I want to print all the members in the 2d array of structures (v) but I am unable to print elements other than 1st row (remaining row elements are printing as 0). Could anyone help how to print members other than 1st row?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 3
    Why the "magic" `10`? – alk Aug 05 '18 at 11:26
  • 1
    Would be a good idea to us braces.Also check the return value from `scanf` – Ed Heal Aug 05 '18 at 12:08
  • 3
    Terminology: you have a 2D *array* of structures, not a "2D structure". – John Bollinger Aug 05 '18 at 12:46
  • for ease of readability and understanding: 1) consistently indent the code. suggest each indent level be 4 spaces. 2) follow the axiom: *only one statement per line and (at most) 1 variable declaration per statement.* – user3629249 Aug 05 '18 at 23:45
  • regarding: `scanf("%s %lf\n",v[i][j].a,&v[i][j].s);` when calling any of the `scanf()` family of functions, always check the returned value (not the parameter values) to assure the operation was successful. 2) when using the input format specifiers '%s' and/or '[..]' always include a MAX CHARACTERS modifier that is one less than the length of input buffer to avoid any chance of a buffer overflow and the resulting undefined behavior – user3629249 Aug 05 '18 at 23:57
  • the posted code contains some 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 10, 20. 'magic' numbers make the code much more difficult to understand, debug, etc. Suggest using `#define` statements or a `enum` statement to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code – user3629249 Aug 05 '18 at 23:59

1 Answers1

6

The problem is occurring because of the way you have declared parameter in print function:

void print(struct name v[][10],int row,int col)
                        ^^^^^^

Due to the innermost dimension 10, in the print() function, the v will be treated as a 2D array whose number of columns are 10 and when you try to print it you will not get the expected output if the value of col is other than 10. Just try once with giving value 10 to n and check the output of your program, you will get the expected output.

To fix this problem, change the sequence of print() function parameter and replace 10 with col

void print(int row, int col, struct name v[][col])

Call it like this

print(m, n, v);

Also, \n is not required in scanf() format string:

scanf("%s %lf\n",v[i][j].a,&v[i][j].s);
             ^^

remove it.

H.S.
  • 11,654
  • 2
  • 15
  • 32
  • You're right that the trailing newline in the scan format string is highly undesirable. One of the canonical questions on the topic is [What is the effect of trailing white space in a `scanf()` format string?](http://stackoverflow.com/questions/19499060/) – Jonathan Leffler Aug 05 '18 at 17:32