Asked  7 Months ago    Answers:  5   Viewed   34 times

I have been struggling for a few hours with all sorts of C tutorials and books related to pointers but what I really want to know is if it's possible to change a char pointer once it's been created.

This is what I have tried:

char *a = "This is a string";
char *b = "new string";

a[2] = b[1]; // Causes a segment fault

*b[2] = b[1]; // This almost seems like it would work but the compiler throws an error.

So is there any way to change the values inside the strings rather than the pointer addresses?



When you write a "string" in your source code, it gets written directly into the executable because that value needs to be known at compile time (there are tools available to pull software apart and find all the plain text strings in them). When you write char *a = "This is a string", the location of "This is a string" is in the executable, and the location a points to, is in the executable. The data in the executable image is read-only.

What you need to do (as the other answers have pointed out) is create that memory in a location that is not read only--on the heap, or in the stack frame. If you declare a local array, then space is made on the stack for each element of that array, and the string literal (which is stored in the executable) is copied to that space in the stack.

char a[] = "This is a string";

you can also copy that data manually by allocating some memory on the heap, and then using strcpy() to copy a string literal into that space.

char *a = malloc(256);
strcpy(a, "This is a string");

Whenever you allocate space using malloc() remember to call free() when you are finished with it (read: memory leak).

Basically, you have to keep track of where your data is. Whenever you write a string in your source, that string is read only (otherwise you would be potentially changing the behavior of the executable--imagine if you wrote char *a = "hello"; and then changed a[0] to 'c'. Then somewhere else wrote printf("hello");. If you were allowed to change the first character of "hello", and your compiler only stored it once (it should), then printf("hello"); would output cello!)

Tuesday, June 1, 2021
answered 7 Months ago

Binary   Oct  Dec    Hex    Abbr    Unicode  Control char  C Escape code   Name
0000000  000  0      00     NUL     ?       ^@                          Null character

There's no difference, but the more idiomatic one is ''.

Putting it down as char c = 0; could mean that you intend to use it as a number (e.g. a counter). '' is unambiguous.

Tuesday, June 22, 2021
answered 6 Months ago

You can use the return keyword: move the nested loop into a subroutine, invoke the subroutine to run the nested loops, and 'return' from the subroutine to exit [all] the loops.

Monday, August 2, 2021
answered 4 Months ago

GetHashCode() is not what you should be using to hash a string, almost 100% of the time. Without knowing what you're doing, I recommend that you use an actual hash algorithm, like SHA-1:

using(System.Security.Cryptography.SHA1Managed hp = new System.Security.Cryptography.SHA1Managed()) {
    // Use hp.ComputeHash(System.Text.Encoding.ASCII (or Unicode, UTF8, UTF16, or UTF32 or something...).GetBytes(theString) to compute the hash code.

Update: For something a little bit faster, there's also SHA1Cng, which is significantly faster than SHA1Managed.

Thursday, August 5, 2021
answered 4 Months ago

You can write parts of your extension in C if you want, but you'll need at least a small bit of C++ code to glue together your C code with Node.

As you will have seen in your HelloWorld, extensions rely on the v8.h and node.h headers, which have all of the classes that Node expects. Without those, you won't be able to properly create the JS object to export back to Node.

That said, you can pretty easily just write a small set of C++ functions that just call C functions, and wrap some kind of C structure.

Monday, August 9, 2021
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :