1

It was difficult to title the question.

Essentially I have an array with it's own structure.

typedef struct Video
{
    unsigned id;
    char title[90];
    char producer[60];
}Video;

Video arrayVideo[7];

int main()
{
    scanf("%s", arrayVideo[0].title);
    printf("%s", arrayVideo[0].title);
}

This code will printout whatever I write during the scanf process.

This small change to int main however;

 int main()
{
    arrayVideo[0].title[90] = ("Hello");
    printf("%s", arrayVideo[0].title);
}

Turns up nothing. The console just outputs the execution time and the normal business. Doesn't output "Hello"

3
  • 1
    you need to use strcpy() to copy array. Commented Jun 28, 2014 at 15:29
  • Do you get compile warnings on that code? You're trying to assign a string constant (a pointer) to a character, and one off the end of the array at that. Commented Jun 28, 2014 at 15:31
  • type of arrayVideo[0].title[90] is char and Next to the last element of the array. Commented Jun 28, 2014 at 15:37

2 Answers 2

2

This issues really stems from a confusion about what pointers do in regards to strings. char title[90] is an array of characters that stores 90 chars. title is a pointer to the first element of that array and title[90] is a pointer to one past the end of the array (because array indexing starts at 0, title[89] is the last element)

So if we break down what's going on in this line:

    arrayVideo[0].title[90] = ("Hello");

The left hand side is a pointer type, it points to the first element in arrayVideo which is a structure and then within that structure points to the character that's 90 elements past the first element of title, note that this is the 91s element. The right hand side is a string literal of const char* type. Currently this code is just assigning a pointer one past the end of your array to point to the address of the start of the "hello" string literal. It does not copy the contents of the string into your data structure as you would have hoped. I'm slightly surprised you didn't get a warning from your compiler when you compiled this, try compiling with all warnings enabled.

To actually copy the contents of the string correctly you need to use strcpy like so:

strcpy(arrayVideo[0].title,"hello")

Here the destination for the strcpy is the start of your title array and the source is from the const char* string literal "hello".

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the detailed explanation! I don't seem to get any warnings telling me of this which confused me further. Could you explain the difference between char* and char?
@PejmanPoh, you might need to enable warnings with your compiler, with gcc this is the -Wall flag. With other compilers it will be a different name, read the manual for your compiler. The difference between char and char* is that char is a character and char* is a location in memory that contains a char. Read a tutorial or introduction about pointers, it will really help further your understanding here.
2

The second one gives you two warnings which tell you what is going wrong:

t.c: In function ‘main’:
t.c:14:29: warning: assignment makes integer from pointer without a cast [enabled by default]
     arrayVideo[0].title[90] = ("Hello");
                             ^
t.c:14:24: warning: array subscript is above array bounds [-Warray-bounds]
     arrayVideo[0].title[90] = ("Hello");
                        ^

The first warning tells you that you're trying to store a pointer (to the string "Hello") into a single char of the title array, and the second tells you that its out of bounds anyways, so might do just about anything, though in this case it probably just writes into the first character of the producer array.

Moral: ALWAYS enable all warnings that you can with your compiler, and pay attention to them.

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.