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
Can TypeScript be updated automatically?
This means there's no way to automatically upgrade the TypeScript version used by VS Code. You can however override the TypeScript version VS Code uses by modifying either the user settings or the workspace settings.
What TypeScript version is VS Code using?
When you open a TypeScript file, VS Code should display the TypeScript version in the status bar at the bottom right of the screen:
Changing the global TypeScript version
npm install -g typescript@2.0.5
Open User Settings
)"typescript.tsdk": "{your_global_npm_path}/typescript/lib"
you can find out {your_global_npm_path} by typing npm root -gNow all of the projects you open with VS Code will use this TypeScript version, unless of course there is a workspace setting that overrides this.
Changing the local TypeScript version
Install the desired TypeScript version locally, for example
npm install --save-dev typescript@2.0.5
The
--save-dev
will update your project'spackage.json
, adding the TypeScript version you installed as adevDependency
.Open VS Code Workspace Settings (F1 >
Open Workspace Settings
)Update/Insert
"typescript.tsdk": "./node_modules/typescript/lib"
Now only the project you installed this TypeScript version in will use that TypeScript version, the global installation will be ignored by VS Code in this project.
Having added the
typescript.tsdk
entry it's then also necessary to use the VS Code UI to select the new version:Click on the version displayed in the VS Code footer:
Select it in the UI:
See also:
typescript.tsdk
to make workspace usage clearer