Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Invalid Array Assignment/Array of Struct

EDIT: PROBLEM SOLVED JUST HAD TO USE STRCPY() THANKS FOR THE HELP!

It has been a while since i've messed with structs in C so the solution to my problem may be very simple but I just can't figure it out. I would usually ask questions like this on stackexchange but the people over there are jerks so I figured I would rely on this community instead.

so I have the following struct declared

Code
typedef struct list{
   char name[256];
   int size;
}list;

//I then init the 2 with integers I get from figuring out the # of files in a directory

list directories[numOfDirectories];  
list files[numOfFiles];

//then inside of a loop I try assigning values to them.

directories [dirCount++].name = de->d_name;
files[fileCount++].name = de->d_name

The last 2 lines are the one where the Invalid Array Assignment error is coming up. I don't think its a problem with the dirent struct (which is assigned to de) as i've used it to print the names of the files. Here is a link to the entire code, thanks in advance for any help.

https://pastebin.com/z9KBwYAG
What is your compiler command line? VLA's are C99+ only.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
i just do c++ main.cpp on linux mint
C++ doesn't have VLA's either - it's a C only feature. You need to allocate a pre-defined amount on the stack or use the heap.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Sep 9, 2017 02:30 PM)0xDEAD10CC Wrote:  C++ doesn't have VLA's either.

I figured it out, i had to use strcpy() instead of setting 2 char arrays equal to each other. Thanks for the help though.
(Sep 9, 2017 02:30 PM)Tralen Wrote:  I figured it out, i had to use strcpy() instead of setting 2 char arrays equal to each other. Thanks for the help though.

That's not even closely related to the original question you asked about the arrays.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Sep 9, 2017 02:32 PM)0xDEAD10CC Wrote:  That's not even closely related to the original question you asked about the arrays.

I just gave the error message and my code. Error message was invalid array assignment, and that is what solved it. What did you think my original question was?
You mentioned compiling as C++ and I didn't scroll down to look past the array declarations. You shouldn't be using strcpy, you should be using strncpy.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by gurlhackerclubMay 19, 2020 05:18 PM 8 250 May 19, 2020 05:18 PM
Last Post: k1r3n
[C] by xx1ben99xxApr 30, 2020 09:05 PM 22 546 Apr 30, 2020 09:05 PM
Last Post: 0xDEAD10CC
[C] by VolitairNov 11, 2018 08:53 AM 1 133 Nov 11, 2018 08:53 AM
Last Post: HFDone