Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

simple c program on structure


  • Please log in to reply

#1
prince01234

prince01234

    New Member

  • Member
  • Pip
  • 3 posts
#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

Edited by prince01234, 02 February 2012 - 11:01 AM.

  • 0

Advertisements


#2
modernsavage

modernsavage

    Retired Staff

  • Retired Staff
  • 51 posts
You don't need the struct. You're not using it.
  • 0

#3
prince01234

prince01234

    New Member

  • Topic Starter
  • Member
  • Pip
  • 3 posts
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
  • 0

#4
prince01234

prince01234

    New Member

  • Topic Starter
  • Member
  • Pip
  • 3 posts
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
  • 0

#5
Spike

Spike

    nOoB

  • Member
  • PipPipPipPip
  • 1,357 posts

#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
  • 0

#6
anuvab1911

anuvab1911

    Member

  • Member
  • PipPip
  • 50 posts
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.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP