Hack Forums
[C] Invalid Array Assignment/Array of Struct - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C] Invalid Array Assignment/Array of Struct (/showthread.php?tid=5714264)



Invalid Array Assignment/Array of Struct - Tralen - Sep 9, 2017

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


* - 0xDEAD10CC - Sep 9, 2017

What is your compiler command line? VLA's are C99+ only.


* - Tralen - Sep 9, 2017

i just do c++ main.cpp on linux mint


* - 0xDEAD10CC - Sep 9, 2017

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.


* - Tralen - Sep 9, 2017

(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.


* - 0xDEAD10CC - Sep 9, 2017

(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.


* - Tralen - Sep 9, 2017

(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?


* - 0xDEAD10CC - Sep 9, 2017

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.