simple c program on structure - Geeks to Go Forums

Jump to content

Log in Register Register Malware removal guide How it works

simple c program on structure

#1 prince01234

  • Group: Member
  • Posts: 3
  • Joined: 02-February 12

Posted 02 February 2012 - 10:52 AM

#include<stdio.h>
#include<conio.h>
void main(){
struct rtu{char[10] name;}rty1[10];
scanf("%s",&rty1[0].name[0]);
printf("%s",rty1[0].name[0]);
getch();
}

now listen in output screen i am getting error -
m termination with a arrow shaped figure
please help guyz

#2 modernsavage

  • Group: Retired Staff
  • Posts: 51
  • Joined: 13-September 05

Posted 02 February 2012 - 11:49 AM

You don't need the struct. You're not using it.

#3 prince01234

  • Group: Member
  • Posts: 3
  • Joined: 02-February 12

Posted 02 February 2012 - 10:30 PM

now what did u mean by i am not using struct here i had declared structure corectly then why i am getting this problem
please tell me how could i remove this error

#4 prince01234

  • Group: Member
  • Posts: 3
  • Joined: 02-February 12

Posted 02 February 2012 - 10:32 PM

now what did u mean by i am not using struct here i had declared structure corectly then why i am getting this problem
please tell me how could i remove this error

#5 spike_hacker_inc

  • Group: Member
  • Posts: 1,328
  • Joined: 27-April 05

Posted 03 February 2012 - 12:27 PM

View Postprince01234, on 02 February 2012 - 10:52 AM, said:

#include<stdio.h>
#include<conio.h>
void main(){
struct rtu{char[10] name;}rty1[10];
scanf("%s",&rty1[0].name[0]);
printf("%s",rty1[0].name[0]);
getch();
}

now listen in output screen i am getting error -
m termination with a arrow shaped figure
please help guyz

Hey there prince,

Just went over your code... Even though your program will compile, it's best practice making your main sub an integer return function.
int main()
{
    printf("Hello world!\n");
    return 0;
}

I have also noticed you did not declare your "name" array correctly. The appropriate syntax is:
char name[10];

When getting input from the user you are trying to store it at the memory address which will not be needed. Access the variable directly would be more efficient for this one.
You also seem to be trying to store the inputted text into "name" position 0, which wouldn't work out too well. Instead think of it as a single variable just storing a variable of 10 characters.
scanf("%s", rty1[0].name);

I have no idea why you using the getch() function, but here is the revised code that works. This is based upon the code you gave and has just been fixed to compile properly.
#include <stdio.h>
#include <conio.h>

int main()
{
    struct rtu
    {
        char name[10];
    } rty1[10];

    scanf("%s", rty1[0].name);
    printf(rty1[0].name);
    getch();

    return 0;
}

I can't give you an exact reason why your program was not compiling properly without exact error codes. But my guess is that your syntax was wrong and you weren't declaring variables correctly... If you give us more information on exactly what it is you wan't to do, it will be easier to aid you.

Peace Out

#6 anuvab1911

  • Group: Member
  • Posts: 50
  • Joined: 17-June 11

Posted 09 February 2012 - 12:32 PM

Well,your definition of the structure seems incorrect.
struct rtu //correct
{
char[10] name //doesn't seem to make sense
};

Properly

struct rtu
{
char name[10]; //this indicates a string name of length 10
} rty1[10];

While scanning,you should use
rty1[0].name


Else it will scan only the first character of the string you input.
And the output as you have written would print only the first character of the string.

Share this topic: