Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Structure problems. . .

RaizoRaizo Member Posts: 131
Hello,
I'm just getting used to suctures, and I'm a bit confused. I can't find out how to initialize a string. Say in the eample structure:

struct school {
char first[20];
char second[20];
char third[20];
} teachers;

How can " teachers.first " be initialized to " Johnson "?
I've tried " *teachers.first = ... " but that creates one letter and then garbage values. Is there a problem in the structure definition?
Am I accessing the member incorrectly? What is wrong?

Comments

  • DB1DB1 Member Posts: 1,142
    : Hello,
    : I'm just getting used to suctures, and I'm a bit confused. I can't find out how to initialize a string. Say in the eample structure:
    :
    : struct school {
    : char first[20];
    : char second[20];
    : char third[20];
    : } teachers;
    :
    : How can " teachers.first " be initialized to " Johnson "?
    : I've tried " *teachers.first = ... " but that creates one letter and then garbage values. Is there a problem in the structure definition?
    : Am I accessing the member incorrectly? What is wrong?
    :


    strcpy(teachers.first, "Johnson");
  • Geeth AsokanGeeth Asokan Member Posts: 127
    : : Hello,
    : : I'm just getting used to suctures, and I'm a bit confused. I can't find out how to initialize a string. Say in the eample structure:
    : :
    : : struct school {
    : : char first[20];
    : : char second[20];
    : : char third[20];
    : : } teachers;
    : :
    : : How can " teachers.first " be initialized to " Johnson "?
    : : I've tried " *teachers.first = ... " but that creates one letter and then garbage values. Is there a problem in the structure definition?
    : : Am I accessing the member incorrectly? What is wrong?
    : :
    :
    :
    : strcpy(teachers.first, "Johnson");
    :

    In order to use [b]strcpy( , )[/b], you also need the header file [b]#include[/b]

  • DB1DB1 Member Posts: 1,142
    : : : Hello,
    : : : I'm just getting used to suctures, and I'm a bit confused. I can't find out how to initialize a string. Say in the eample structure:
    : : :
    : : : struct school {
    : : : char first[20];
    : : : char second[20];
    : : : char third[20];
    : : : } teachers;
    : : :
    : : : How can " teachers.first " be initialized to " Johnson "?
    : : : I've tried " *teachers.first = ... " but that creates one letter and then garbage values. Is there a problem in the structure definition?
    : : : Am I accessing the member incorrectly? What is wrong?
    : : :
    : :
    : :
    : : strcpy(teachers.first, "Johnson");
    : :
    :
    : In order to use [b]strcpy( , )[/b], you also need the header file [b]#include[/b]
    :
    :
    or: [b]#include [/b]
  • RaizoRaizo Member Posts: 131
    : : : : Hello,
    : : : : I'm just getting used to suctures, and I'm a bit confused. I can't find out how to initialize a string. Say in the eample structure:
    : : : :
    : : : : struct school {
    : : : : char first[20];
    : : : : char second[20];
    : : : : char third[20];
    : : : : } teachers;
    : : : :
    : : : : How can " teachers.first " be initialized to " Johnson "?
    : : : : I've tried " *teachers.first = ... " but that creates one letter and then garbage values. Is there a problem in the structure definition?
    : : : : Am I accessing the member incorrectly? What is wrong?
    : : : :
    : : :
    : : :
    : : : strcpy(teachers.first, "Johnson");
    : : :
    : :
    : : In order to use [b]strcpy( , )[/b], you also need the header file [b]#include[/b]
    : :
    : :
    : or: [b]#include [/b]
    :
    Thank you very much, working exactly right now.
  • ShkaboinkaShkaboinka Member Posts: 79
    : Hello,
    : I'm just getting used to suctures, and I'm a bit confused. I can't find out how to initialize a string. Say in the eample structure:
    :
    : struct school {
    : char first[20];
    : char second[20];
    : char third[20];
    : } teachers;
    :
    : How can " teachers.first " be initialized to " Johnson "?
    : I've tried " *teachers.first = ... " but that creates one letter and then garbage values. Is there a problem in the structure definition?
    : Am I accessing the member incorrectly? What is wrong?
    :
    uh, ever think of making a CONSTRUCTOR???

  • Justin BibJustin Bib USAMember Posts: 0

    _____ ( http://forcoder.org ) free ebooks and video tutorials about ( PL/SQL PHP C# Perl Objective-C Java Visual Basic Scratch JavaScript Visual Basic .NET Swift Ruby Assembly C R MATLAB Delphi C++ Go Python VBScript Lisp FoxPro Logo Scala LabVIEW COBOL Kotlin Fortran Transact-SQL D Alice Crystal Rust ABAP Dart ML Apex Erlang SAS Clojure Julia Lua F# Prolog Bash Awk Ada Hack Scheme ) _____

Sign In or Register to comment.