-2

There isn't a single webpage that explains how I can use make struct like this and can only find an example in my book. So I'm trying it out and it wont save or print anything at all.

//array struct?
#include <stdio.h>
#include <string.h>

struct testing
{
    int a;
    int b;
    char c[5];
} t[3];

void main()
{
    struct testing;
    t[1].a = 10;
    t[1].b = 20;
    strcpy("thing", t[1].c);

    printf("%d %d %s", t[1].a, t[1].b, t[1].c);
}

as you can see, t[1].a is suppose to store 10. That printf doesn't print a single thing. Am I doing this wrong?

lmgesus
  • 9
  • 4
  • 3
    Look up the order of arguments to `strcpy`. The destination comes first. Also, a string literal `"thing"` has size `6` not `5`. – Cheatah Nov 14 '22 at 07:27
  • 1
    Try running your program in a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – Some programmer dude Nov 14 '22 at 07:28
  • 2
    On an unrelated thing, what do you think the statement `struct testing;` does? – Some programmer dude Nov 14 '22 at 07:29
  • 1
    OT: `void main()` --> `int main(void)` – Support Ukraine Nov 14 '22 at 07:31
  • @Cheatah I don't understand, how does messing up the data size for ```c``` variable mess up everything else? – lmgesus Nov 14 '22 at 07:31
  • @Someprogrammerdude I assume it calls the struct data type – lmgesus Nov 14 '22 at 07:35
  • The problem here is not the size. The problem is that you are using a string literal (i.e. "thing") as destination for `strcpy`. That is illegal. Further, since you never assign to `t[1].c` it isn't safe to print it. The size will however be a problem once you fix the code to be `strcpy(t[1].c, "thing");` .... then there is too little memory to hold a copy of "thing". Remeber that C strings require an extra char to hold the string termination charaqcter – Support Ukraine Nov 14 '22 at 07:36
  • 1
    There's no such thing as "calling" a structure type. It's equivalent to a forward declaration of the structure name, but since it's already been declared (and defined) above there's no use of it. – Some programmer dude Nov 14 '22 at 07:49
  • 1
    @imgesus Maybe you don't understand because you focus only on the second half of my comment. Let's focus on the *first* half first. Did you look up the order of arguments to `strcpy`? Do you understand what we mean by that? – Cheatah Nov 14 '22 at 07:51
  • @lmgesus `strcpy("thing", t[1].c);` -> `strcpy(t[1].c, "thing");`. But this isn't gong to end well either, `"thing"` needs 6 bytes, not 5, you need to take the null terminator into account. – Jabberwocky Nov 14 '22 at 07:55
  • 2
    @lmgesus _"how does messing up the data size for c variable mess up everything else"_: you have a buffer overflow here, this will lead to _undefined bahaviour_ which includes "messing up evreything else". – Jabberwocky Nov 14 '22 at 07:58
  • @Jabberwocky how does buffer overflow at a later line affect codes that are above. or is this something I have to study cs for because i thought codes that are written physically above are executed first, hence a and b gets assigned 10 and 20 respectively first. – lmgesus Nov 15 '22 at 05:40
  • @lmgesus google "undefined behaviour C". During a buffer overflow you write beyond the memory allocated to your buffer/array. This memory might be unused, it might belong to another variable of your program, it might be a system variable etc. – Jabberwocky Nov 15 '22 at 07:51

1 Answers1

0

Here is the code, with fixes applied and commented:

//array struct?
#include <stdio.h>
#include <string.h>

struct testing
{
    int a;
    int b;
    char c[6]; /* increased space to fit "thing" with terminator */
} t[3];

int main(void) /* changed return type to int, and args to void */
{
    t[0].a = 10; /* the first instance is at 0, C indexes from zero */
    t[0].b = 20;
    strcpy(t[0].c, "thing"); /* corrected argument order, and index 0 */

    printf("%d %d %s", t[0].a, t[0].b, t[0].c); /* use index 0 */
    return 0; /* added missing return */
}
unwind
  • 391,730
  • 64
  • 469
  • 606
  • I dont understand why people are telling me to use ```int main(void)``` instead of ```void main()``` what am I missing out on by not using the former? – lmgesus Nov 16 '22 at 09:28
  • The language standard says that `main()` returns `int`. It's possible for implementations to support/allow other return types, but I don't see the point in using non-standard code in answers to programming questions, that just adds more confusion. – unwind Nov 16 '22 at 09:45