How can I point this string to the structure?

Related searches
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


    typedef struct{
    char firstName[20];
    char lastName[20];
    int id; 
    char gender[10]; 
    int monthOfBirth; 
    int dayOfBirth; 
    int yearOfBirth; 
    } HealthProfile;


void setName(HealthProfile *HP) {
    char firstName;
    char lastName; 
    printf("Enter your first and last name: ");
    scanf("%s" "%s", &firstName, &lastName);
    HP->firstName = firstName;
    HP->lastName = lastName; 
}

void setID(HealthProfile *HP) {
    int id; 
    printf("Enter ID: ");
    scanf("%d", &id);
    HP->id = id;
}

void setGender(HealthProfile *HP) {
    char gender;
    // HealthProfile hp;
   // strcpy(hp.gender, &gender); 
    printf("Enter your gender: ");
    scanf("%s", &gender);

    HP->gender = gender;

}
void setBD(HealthProfile *HP) {
    int monthOfBirth;
    int dayOfBirth;
    int yearOfBirth; 
    printf("Enter your month of birth, day of birth and year of birth: ");
    scanf("%d" "%d" "%d", &monthOfBirth, &dayOfBirth, &yearOfBirth);
    HP->monthOfBirth = monthOfBirth;
    HP->dayOfBirth = dayOfBirth;
    HP->yearOfBirth = yearOfBirth; 
}

int main(){
    // pointer declared to HealthProfile structure 
    HealthProfile *HP;
    // pointer initialized using malloc 
    HP = (HealthProfile*) malloc(sizeof(HealthProfile));

// Calls various functions

    setID(HP); 
    setGender(HP);
    setName(HP);
    setBD(HP);

    // Creates your profile
    printf("Creating your Health Profile! \n");
    printf("Profile created for: %s\n", HP->firstName);
    printf("Lastname: %s\n", HP->lastName);
    // prints ID 
    printf("ID: %d\n", HP->id);
    printf("Gender: %s\n", HP->gender);
    printf("Month of birth: %d\n", HP->monthOfBirth); 
    printf("day of birth: %d\n", HP->dayOfBirth);
    printf("year of birth: %d\n", HP->yearOfBirth);
} 

What I am trying to do is assign HP to a string but I get this error.

array type 'char [10]' is not assignable
    HP->gender = gender;

(same error with first and last name of course) So I searched online and found out that char can't be assigned and strcpy should be used instead. As you can see by my failed attempt in the gender function.

Can someone help me fix my errors? Thanks

As you've discovered, you can't assign directly to an array. For strings in particular, you would need to use strcpy to copy a string from one array to another.

Additionally, gender is a single character, not an array. So passing &gender to scanf will cause the function to treat it as a pointer to a sequence of characters instead of a pointer to just one, resulting in the function writing past the memory bounds of the variable.

You can fix this by making gender an array of the proper size:

void setGender(HealthProfile *HP) {
    char gender[10];
    printf("Enter your gender: ");
    scanf("%s", gender);
    strcpy(HP->gender, gender);
}

Or you could skip the temp variable entirely and write directly to the field in the struct:

void setGender(HealthProfile *HP) {
    printf("Enter your gender: ");
    scanf("%s", HP->gender);
}

And do the same for reading firstname and lastname.

Pointer to a Structure in C, We have already learned that a pointer is a variable which points to the So we can't assign a new string to it using assignment operator ( = )� At this point ptr_dog points to the structure variable spike, so by dereferencing it we will get the contents of the spike. This means spike and *ptr_dog are functionally equivalent. To access a member of structure write *ptr_dog followed by a dot(.) operator, followed by the name of the member. For example: (*ptr_dog).name - refers to the name

char gender; - gender is not an array. It is only one object of type char.

Thus, HP->gender = gender; does not work because gender does not decay to a pointer to char like an array would do.

Edit it to char gender[10].

Structures in C, #include <stdio.h> #include <string.h> int main() { struct student { int roll_no; char ptr = &stud; → We are making our pointer 'ptr' to point to structure 'stud'. Dim xString As String = xStructToString(myInstance) Public Function xStructToString(ByVal obj As Object) As String Dim x As New Xml.Serialization.XmlSerializer(obj.GetType) Dim sw As New IO.StringWriter() x.Serialize(sw, obj) Return sw.ToString End Function But I can't get data back from string to structure.

actually I think that you should not using Assignment for string and array .Instead you should use strcpy and include string.h library.

C/Structs, When you declare a variable with type struct string, the compiler allocates enough We can then refer to elements of the struct string that sp points to (i.e. s ) in� A pointer variable can be created not only for native types like (int, float, double etc.) but they can also be created for user defined types like structure.. If you do not know what pointers are, visit C++ pointers.

C - Structures, C - Structures - Arrays allow to define type of variables that can hold several data items #include <stdio.h> #include <string.h> struct Books { char title[50]; char� Here's how you can create pointers to structs. struct name { member1; member2; . . }; int main() { struct name *ptr, Harry; } Here, ptr is a pointer to struct.

How can I access structures through strings?, structures through strings?. Learn more about structures, inca, etas. arrays to go through the. %same number of points as the smaller array. Equals (Point, Point) Compares two Point structures for equality. Get Hash Code () Returns the hash code for this Point. Multiply (Point, Matrix) Transforms the specified Point structure by the specified Matrix structure. Offset (Double, Double) Offsets a point's X and Y coordinates by the specified amounts.

&#39;Struct&#39; keyword is used to create a structure. A structure can contain variables, methods, static constructor, parameterized constructor, operators, indexers, events, and property. A structure can not derive/inherit from any structure or class. A structure can implement any number of interfaces. Structures provide better performance.

Comments
  • char gender; - gender is not an array. It is only an object of type char. Edit it to char gender[7].
  • Same is applicable here HP->firstName = firstName;. firstly you wanted firstName to be of char array type not just a char type, then you can't assign directly one array with another. Try strncpy(HP->firstName, firstName, MAX_LENGTH);
  • You really don't want fixed-length buffers in structs. Consider using char* and dynamic allocation if necessary.
  • when using scanf into fixed buffers your should ALWAYS specify a length limit: scanf("%9s", gender); to avoid overflowing the buffer.