#include <stdio.h>
#include <stdlib.h>


typedef struct {
  int finalMark; /* Ï ãåíéêüò âáèìüò ãéá çï ìÜèçìá */
}StudentType;





struct var {
    
    StudentType *aStudent;
    char onomaa[20];
    char eponimoa[20];
};
typedef struct var newtype;

newtype a;


main()

{
  int bathmos;
  char onoma[20];
  scanf("%s",&onoma);
  strcpy(a.onomaa,onoma);
  printf("%s",a.onomaa);
  system("pause");
  scanf("%d",bathmos);
  strcpy(a.aStudent->finalMark,bathmos);
  printf("%d",a.aStudent->finalMark);
  system("pause");
}

this is my problem and the title is problem anyone can help :S? i am new at c so keep it simple thanks :)

Recommended Answers

All 10 Replies

Right away you should include the string.h library for strcpy().

This line

strcpy(a.aStudent->finalMark,bathmos);

Both variables are integers, strcpy requires char pointers.

Your first scanf

scanf("%s",&onoma);

should be

scanf("%s", onoma);

Right away you should include the string.h library for strcpy().

This line

strcpy(a.aStudent->finalMark,bathmos);

Both variables are integers, strcpy requires char pointers.

Your first scanf

scanf("%s",&onoma);

should be

scanf("%s", onoma);

this part works fine

scanf("%s",&onoma)

the problem is here when i run the program it just stops working and freezes :S

strcpy(a.aStudent->finalMark,bathmos);

This is what my help files say about strcpy

STRCPY(3) Linux Programmer's Manual STRCPY(3)

NAME
strcpy, strncpy - copy a string

SYNOPSIS
#include <string.h>

char *strcpy(char *dest, const char *src);

char *strncpy(char *dest, const char *src, size_t n);

DESCRIPTION
The strcpy() function copies the string pointed to by src, including
the terminating null byte ('\0'), to the buffer pointed to by dest.
The strings may not overlap, and the destination string dest must be
large enough to receive the copy.

strcpy copies strings not integers.

Also you have more than one problem, here's the output when I tried to compile your program...I called it testit.c

testit.c:24:1: warning: return type defaults to ‘int’
testit.c: In function ‘main’:
testit.c:29:3: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘char (*)[20]’
testit.c:30:3: warning: implicit declaration of function ‘strcpy’
testit.c:30:3: warning: incompatible implicit declaration of built-in function ‘strcpy’
testit.c:33:3: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
testit.c:34:3: warning: passing argument 1 of ‘strcpy’ makes pointer from integer without a cast
testit.c:34:3: note: expected ‘char *’ but argument is of type ‘int’
testit.c:34:3: warning: passing argument 2 of ‘strcpy’ makes pointer from integer without a cast
testit.c:34:3: note: expected ‘const char *’ but argument is of type ‘int’
testit.c:37:1: warning: control reaches end of non-void function
testit.c:33:8: warning: ‘bathmos’ is used uninitialized in this function

i got a bit confused the problem i figured out i have is that program crushes when i try to use strcpy with a struct which is inside another struct.Can you help me with that?

i got a bit confused the problem i figured out i have is that program crushes when i try to use strcpy with a struct which is inside another struct.Can you help me with that?

Are you reading what I posted...strcpy copies strings not integers...Your tying to copy integers with strcpy.

strcpy(a.aStudent->finalMark,bathmos);

a.aStudent->finalMark is an integer
bathmos is an integer

just do this

a.aStudent->finalMark = bathmos;

I did what you said which i agree is correct but the program still crashes when it reaches the point to do anything with (in this example finalMark) which is in a struct and that struct is in another one :S.That's the problem

Your structure data member aStudent

StudentType *aStudent;

Did you allocate/assign memory for the pointer?

a.aStudent=(StudentType*)malloc(sizeof(StudentType));

i typed this but the program still crashes

Your scanf on line 33 ..you have made a tiny mistake ...
Is this how you take an integer as input ?

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.