Hi everybody.
I'm new to coding C.I can already code in VB and ASM,but after spending a lot of time coding in VB its really hard for me to learn to code in C.Especially pointers are really confusing.I was wondering,if someone could please check the following code and tell me what i'm doing wrong with it:
Code:
#include <stdio.h>
/*Declare record as a struct:
*/
struct record
{
char name[100];
int age;
char key [25];
};
/*The following function is used for creating a record of
some names,their ages,and a key which would be used to refer to
them.
*/
struct record create(int n,struct record*field)
{
int x;
int a;
char na[100],k[25];
/*X is the int which'll be used in the for loop.Na is a
temporary varible that holds the value of the name
entered and then passes it to the record.Well it tries to,
because the compiler throws an error on the line where it
tries to pass the value.K is a temp. variable to hold
the key,and a is the temp. variable to hold the age.Hope it
makes sense!
*/
for (x=0;x<=n;x++)
{
printf("Field # %d:\n",x);
printf("Name: ");
scanf("%s",na);
field->name=na;
/*The above line is where the program execution breaks
when i try to compile the program.When i say the execution
breaks,i mean that the line turns red with a small cross(x)
to its left.
*/
printf("Age: ");
scanf("%d",a);
field->age=a;
printf("Key: ");
scanf("%s",k);
field->key=k;
printf("Field # %d set up.\n\n",x);
return *field;
++ *field;
}
printf("All fields set up.\n");
printf("Total fields set up:%d",n);
}
void show(int n,,struct record field);
/*The above line is used to declare the function
show.The actual function is below main().If i dont
declare a function before name and write down its code
below main,then i get an error.Thats why i declared it here.
I hope i'm not more confusing than pointers are :-D
*/
main()
{
int n;
char yn[3];
printf("Enter the number of records to create: ");
scanf("%d",&n);
printf("OK.\n\n");
struct record field[n];
create(n,&struct record field);
printf("\n\nWould you like to view the records you've created? ");
scanf("%s",yn);
if (yn=="yes")
{
show(n,struct record field);
}
else
{
printf("would you like to create some new records? ");
scanf("%s",yn);
if (yn=="yes")
{
main();
}
else
{
printf("END.");
}
}
}
/*The function below shows the records created.*/
void show(int n,struct record field)
{
int x;
for (x=1;x<=n;x++)
{
printf("Name:%s",field[x].name);
printf("Age:%d",field[x].age);
printf("Key:%s",field[x].key);
}
printf("End of all records.\n\n");
}
When i try compiling this code,i get the following error:
33 C:\WINDOWS\Profiles\****\Desktop\cprojs\testproj\m .c
ISO C++ forbids assignment of arrays
along with a number of other errors.but this one is highlighted when the compilation breaks.
If someone could please help me with it i'd be really grateful.thanks.
@li.