You have
char *array[size];
That makes array
an array of pointers. Specifically, array
is an array of size
pointers. Each element in the array (array[0]
to array[size-1]
) is of type char *
.
I think it would help if you understood how array
works:
+-------+-------+-------+-------+
| [0] | [1] | [2] | [3] |
+-------+-------+-------+-------+
| | | |
| | | |
| | | |
v v v v
Each of the boxes above represents a pointer, and the arrows are where they are pointing to. The storage for them is not yet assigned to, so they are just "out there", pointing nowhere useful. You can either create memory for them (malloc()
etc., in C, new[]
in C++), or you can point them to some existing location.
You say:
...put some strings in it. If I assign something again to them, they don't replace the previous contents but they keep on appending on the previous contents. How do I correctly clear/reset all of its contents?
It is not clear how you are putting "strings in it". Do you mean you are storing strings in array[0]
..array[size-1]
? How? Are you assigning literal strings to them? Something like:
array[0] = "String";
If you are doing that, then, you can reassign to the pointers and the strings wouldn't append. In other words, if later in your program you do:
array[0] = "Another string";
you are reassigning the pointer array[0]
to point to "Another string"
, and thus you're not appending.
So, in short, we need to see more code, and you may need to understand pointers and arrays better.
Edit: Based upon your edit, the pointers myMainArr[i]
(for i=0
to i=3
), do get reassigned to the corresponding elements from myArrOne
or myArrOne
(not a typo!), depending upon the contents of action
. So, if you printed them (for example, printf("%s\n", myMainArr[0]);
), you shouldn't see any strings being appended. Also, myMainArr
is local to your function function
, so it gets destroyed when your function returns. (Incidentally, your choice of the names array
for an array and function
for a function make it harder to be unambiguous when answering the question!)
If you are having a problem, please post a complete, minimal, compilable code that shows the problem.
Best Answer
What is a workspace?
A project that consists of one or more root folders, along with all of the Visual Studio Code configurations that belong to that project. These configurations include:
Why is a workspace so confusing?
Visual Studio Code does not use the term consistently across the UI (I've opened a GitHub issue to address this). Sometimes it refers to a workspace as described above, and other times it refers to a workspace as a project that is specifically associated with a
.code-workspace
file.A good example being the recent files widget. Notice in the linked screenshot that all projects are grouped under the same "workspaces" heading, which would indicate that everything there is a workspace. But then projects with a
.code-workspace
file are given a "Workspace" suffix, contradicting the heading and indicating that only those files are actually workspaces.What is a
.code-workspace
file?It is a JSON file with comments that stores all of the configuration data mentioned above, in addition to the location of all root folders belonging to a workspace.
Do I need a
.code-workspace
file?Only if you're creating a multi-root workspace, in which case you'll have a single
.code-workspace
file that automatically restores all of the workspace settings, in addition to all of the root folders that you want to be displayed in the Explorer.What about single folder projects?
Everything is automated.
When you open a folder in Visual Studio Code and start making modifications to the editor that are specifically related to the project you're currently working on, Visual Studio Code automatically creates a
.vscode
folder and stores it in the root of the project folder that you're working on. This.vscode
folder has files that store the changes you made.For example, if you change Visual Studio Code settings that you want to apply only to your current project, Visual Studio Code creates a
settings.json
file with those updates, and that file is stored in the.vscode
folder.You can create a
.code-workspace
file that includes just a single root folder if you really want to. You'd then be able to either open the project folder directly, or open the workspace file. But I can't think of any reason why this would be beneficial.How do I create a
.code-workspace
file?Go to menu File → Save Workspace As...
How do I add root folders to a workspace?
Go to menu File → Add Folder to Workspace....
How do I open a workspace that is defined by a
.code-workspace
file?Go to menu File → Open Workspace....
Alternatively, double click the
.code-workspace
file. Visual Studio Code won't open the actual file. Instead, it will read that file and open the folders that belong to that workspace.How do I view the actual
.code-workspace
file?Go to menu File → Open... and select the target
.code-workspace
file.Alternatively, open the workspace associated with that file. Then open the command palette, search for, and select the Workspaces: Open Workspace Configuration File command.