0

So I'm trying to learn C right now, and I have some basic struct questions I'd like to clear up:

Basically, everything centers around this snippet of code:

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

#define MAX_NAME_LEN 127

typedef struct {
    char name[MAX_NAME_LEN + 1];
    unsigned long sid;
} Student;

/* return the name of student s */
const char* getName (const Student* s) { // the parameter 's' is a pointer to a Student struct
    return s->name; // returns the 'name' member of a Student struct
}

/* set the name of student s
If name is too long, cut off characters after the maximum number of characters allowed.
*/
void setName(Student* s, const char* name) { // 's' is a pointer to a Student struct |     'name' is a pointer to the first element of a char array (repres. a string)
    s->name = name;
}

/* return the SID of student s */
unsigned long getStudentID(const Student* s) { // 's' is a pointer to a Student struct
    return s->sid;
}

/* set the SID of student s */
void setStudentID(Student* s, unsigned long sid) { // 's' is a pointer to a Student struct | 'sid' is a 'long' representing the desired SID
    s->sid = sid;
}

I've commented up the code in an attempt to solidify my understanding of pointers; I hope they're all accurate.

So anyway, I have a feeling that setName and setStudentID aren't correct, but I'm not exactly sure why. Can someone explain? Thanks!

EDIT:

 char temp
 int i;
 for (i = 0, temp = &name; temp != '\0'; temp++, i++) {
     *((s->name) + i) = temp;
r123454321
  • 3,323
  • 11
  • 44
  • 63

3 Answers3

5

You are not copying the full name array with this

void setName(Student* s, const char* name) { 
   s->name = name;
}

try this

strcpy(s->name,name);

to copy this string to your structs array. You cant simply assign a pointer argument to an array variable like you have currently. You need to copy each character pointed to by name to the elements of your array s->name. This is what strcpy will do - it copies elements from the source to the destination until it finds a terminating null character.

EDIT: Alternatively you could use strncpy as suggested in a comment. Check this question and its answers to see why some people think this is a good idea Why should you use strncpy instead of strcpy?

Community
  • 1
  • 1
mathematician1975
  • 21,161
  • 6
  • 59
  • 101
  • Thanks. So how would I do something like this manually, without strcpy? for (i = &name; i != '\0'; i++)? – r123454321 Sep 09 '12 at 09:50
  • Check the length of the `name` argument (you can do this with `strlen`), then loop over each index copying into your array. Check the length does not exceed your array bound of course. – mathematician1975 Sep 09 '12 at 09:53
  • Thanks! Could I also do this with a 'for' loop, stopping the loop when the current character == '\0'? I've typed up a little snippet of code for what I think it should look like -- I'm almost positive there's some sort of pointer error or something though. I've pasted it at the bottom of my original post. – r123454321 Sep 09 '12 at 09:57
  • Well yes but that is basically how `strcpy` works itself. `strlen` and `strcpy` iterate from the start until they find a char that equals '\0'. If you are new to C do some experimenting, try your way, try strcpy, strncpy, memcpy - you will learn from it especially when you make mistakes. – mathematician1975 Sep 09 '12 at 10:01
3
s->name = name;

Since s->name is an array, you can't assign to it (it's not a modifiable lvalue) - it should be a compiler error. You'll have to strcpy or memcpy into it, but make sure name isn't too large.

cnicutar
  • 178,505
  • 25
  • 365
  • 392
1

setStudentID is perfectly fine, but setStudentName isn't. You're trying to assign a char* to an array, and that doesn't work. You'll have to use a function that copies it element-wise, like strcpy.

Publius
  • 1,184
  • 2
  • 10
  • 27