I would like to access the name field from the array items and print the name but I am having trouble.

I created a pointer 'L' in callInitialize and set it to the upper struct type List which I named 'studentList'.

int callInitialize () {

    /*Initialize a List*/
    List studentList;
    List *L;
    L = &studentList;
    Initialize(L);
    
    #ifdef DEBUG
        printf("L->count after function call Initialize = %d\n", L->count);
        printf("L->items[0].name after function call Initialize = %s\n", studentList.items[0].name);
    #endif 

    return 0;
    }

I then called Initialize and tried to set the value to test but this is incorrect.

void Initialize (List *L) {
    char test = "I like you";
    L->count = 0;
    L->items[0].name = test;
    }

I am unsure of why L->items[0].name = test; is not appropriate. I am getting an incompatible types error but name is char and test is char?

Also, once I do change that value how would I print that? My thinking was that %s would be correct since the type of the field name is char. Print is above in callIntialize as a debug statement.

My struct declarations:

#define MAXNAMESIZE 20
    typedef struct {
    char name[MAXNAMESIZE];
    int grade;   
    } Student;

    typedef Student Item;
    #define MAXLISTSIZE 4
    typedef struct {
    Item items[MAXLISTSIZE];
    int count;
    } List;

Thank you for any help.

Recommended Answers

All 2 Replies

I can't believe you compiler didn't warn you about this line..

char test = "I like you";

Is this what your trying to do?

#include <stdio.h>

#define ARR_SIZE 3

struct mys
{
  char * my_arr[ARR_SIZE];
};

int main()
{
  size_t i = 0;
  struct mys thes;
  
  char * test1 = "I like you";
  char * test2 = "I like you...not";
  char * test3 = "Hello, world!";
  
  thes.my_arr[0] = test1;
  thes.my_arr[1] = test2;
  thes.my_arr[2] = test3;
  
  for (i = 0; i < ARR_SIZE; ++i)
	fprintf(stdout, "ans->%s\n", thes.my_arr[i]);
  return 0;
}

Embarrassingly enough the solution was as simple as using strncpy to change the values I wanted to alter of the array of structs.

Thank you for taking the time to reply.

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.