0

This code works perfectly; as you can see here name is a pointer. When I give a value to it, it is stored perfectly, but when I change the pointer to array the code breaks. Why do I need a pointer to store a string?

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

struct student{
    char *name;
    int roll;
    int clas;
}*ptr,stu;

void main(){
    int i;
    int n;
    stu.roll=2;
    stu.name = "sum";
    printf("%d %s",stu.roll,stu.name);
}

But this doesn't:

#include<stdio.h>
#include<stdlib.h>
struct student{
    char name[10];
    int roll;
    int clas;
}*ptr,stu;

void main(){
    int i;
    int n;

    stu.roll=2;
    stu.name = "sum";
    printf("%d %s",stu.roll,stu.name);
}
4
  • 1
    You don't need a pointer to store a string. You need a memory location to store a string. In the first case, you are assigning the location of the (first element of) the string constant to the pointer, and later using that pointer to access the memory to print the data. In the second case, you have a syntax error because you cannot assign a string constant to an array. Commented Oct 4, 2020 at 15:30
  • @WilliamPursell This might be a stupid question but don't we store strings in character arrays in C? char name[10]="John" doesnot give any error, then why THIS particular problem throws one? If char name[10] = "John" is a bad way to assign strings, what is the BEST way? Commented Oct 5, 2020 at 4:07
  • char name[] = "John"; is not an assignment. It is an initialization. Syntactically, it is a short cut for char name[5] = { 'J', 'o', 'h', 'n', '\0'}. There is no similar syntactic sugar for char name[5]; name = "John"; That is just a syntax error. You have to write char name[5]; strncpy( name, "John", sizeof name); Commented Oct 5, 2020 at 15:07
  • OT: regarding: void main(){ Per the C standard, there are only two(2) valid signatures for main() they are: int main( void ) and int main( int argc, char *argv[] ) Commented Oct 5, 2020 at 16:06

2 Answers 2

2

You cannot assign to arrays. This doesn't have a meaning in C.

You want:

strcpy(stu.name,"sum");
Sign up to request clarification or add additional context in comments.

3 Comments

If so how is this working? #include <stdio.h> void main() { char arr[5] = "Rosha"; printf("%s", arr); }
@sumanrajkhanal The example you give in your comment is undefined behavior, since a string with length 5 cannot fit in an array of size 5.
No undefined behaviour with the initialization - the null terminator will simply be omitted in this case (C11 6.7.9). But the subsequent printf does cause UB. Be careful!
1

You don't need to use a pointer to store strings. Arrays are pointers are two valid ways to deal with strings, and they are some way mutually bound with each other. You just need to understand what an array is and how are characters stored in them so that we can call them "strings".


First thing we need to remember: what is a pointer (to a type T)?

  • It the address where data of type T is stored

Now, what is an array `T var[N]?

  • It is a sequence of N elements of the same type T stored contiguously in memory.
  • var is the name of the array, and represents the address of its first element of the array. It is evaluated like a pointer though it's not a pointer.

Almost there. What is a string?

  • it is an array containing elements of type char terminated by a special nul-terminator character (`'\0')

So a string IS an array, but like every array can be evaluated as a pointer to its first element. And every pointer to char is a string if it is terminated by the null character, and be accessed with the array syntax:

char * str = "hello"; // contains 'h', 'e', 'l', 'l', 'o', '\0'
                      // cannot be modified because it points to a constant area of memory

printf ("%c %c\n", str[1], str[4]); // prints "e o"

/********/

char str2[] = "hello"; // same contents as above, but thi one can be modified

Note: the assignment

stu.name = "sum";

is invalid because name is an array field of the struct student struct. As explained above an array is not a pointer, and one of the main differences is that it cannot be assigned (i.e. cannot be an lvalue in an assignment action). It will raise a compilation error.

The correct action is copying the data into the array using strcpy() function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.