With function strdup (here is an implementation, if you don't have one already)
char *strdup (const char *f)
{
size_t len;
char *to;
len= strlen (f);
to = malloc (len+1);
if (to) memcpy (to, f, len+1);
return to;
}
Btw, this is a C++ question. in 'String str', str is an object and most probabily (based on most String implementations) we can use str = "my name"; int len = str.length(); String s = new String; <== this is something wrong. it should be String *s = new String; here 's' is a pointer and the object is allocated/instanciated by 'new' operator you can use it *s = "my name" int len = s->length()
The strcpy function is declared in the <string.h> header of the C standard library. The function is used to copy a null-terminated string (a null-terminated array of type char). The function accepts two arguments: a pointer to the memory allocation where the copy will be placed; and a pointer to the first character of the null-terminated string to be copied. The memory allocation where the copy will be made must be large enough to accommodate the string, including the null-terminator. Example usage: void f (char* s) { int len; char* c; len = strlen (s) + 1; /* determine length of string (including null-terminator) */ c = malloc (len); /* allocate memory for copy */ strcpy (c, s); /* ... */ free (c); /* release allocation */ }
The difference here is that char *p = "Hello"; will place Hello world in the read-only parts of the memory and making p a pointer to that, making any writing operation on this memory illegal. While doing: char p[] = "Hello"; puts the literal string in read-only memory and copies the string to newly allocated memory on the stack. p[0] = 'A'; is legal.
As this is probably a homework question, I will give you some pseudo code: [code] num_chars = 0 READ ch FROM string WHILE ch IS NOT END OF STRING num_chars = num_chars + 1 READ ch FROM string END WHILE [/code] Remember that in C, we use what are called "C-strings". C-strings are a pointer to a continuous group of characters in memory which are terminated by a null character. The null character is '\0', and has an integer value of 0. The C-string generally points to the first character in the string. To access the value of this character, you must use the dereferencing operator, *. If you want to move to the next character, you simply add 1 to the pointer. So if you have a C-string: char *str = "abcd"; then: *str '\0' Anything past the null character is undefined. Trying to access this data is considered to be a buffer overflow, and is very dangerous. Note that c-strings created as pointers should always be treated as immutable, as trying to change them might produce errors. Many compilers will allocate the above string inside the static data area, along with any constants or literals which can not fit inside the immediate field of an instruction. If you want a mutable string, then declare it as a character array: char str[] = "abcd"; This method of declaration will explicitly allocate memory on the stack to store the c string in, and as such, the string can be safely manipulated without fear of unintended side effects.
Yes, the if the two string objects point to the same memory location. But "==" is not the best way to compare two string objects. Two strings can be compared using obj1.equals(obj2). This compares for the textual equal-ness of the two string objects.
char* duplicated strdup("Hello World"); duplicated will now contain the address to dynamically allocated memory of a C-style string that contains "Hello World" and a null at the end. Be sure to free duplicated when you're done with it or you'll have a memory leak.
Btw, this is a C++ question. in 'String str', str is an object and most probabily (based on most String implementations) we can use str = "my name"; int len = str.length(); String s = new String; <== this is something wrong. it should be String *s = new String; here 's' is a pointer and the object is allocated/instanciated by 'new' operator you can use it *s = "my name" int len = s->length()
Sure, you can write a function in C to convert a string to Pig Latin without using pointers by passing the string as a parameter and manipulating it directly within the function. You can split the string into words, check if a word starts with a vowel or consonant, and then apply the appropriate transformation following the rules of Pig Latin. Remember to allocate enough memory for the modified string to prevent buffer overflow.
The purpose of the head string pool in a programming language is to store and manage commonly used string literals in memory. This helps reduce memory usage by reusing the same string objects instead of creating new ones every time they are used. This can improve memory management by reducing the amount of memory allocated for storing duplicate string literals, leading to more efficient use of memory resources.
Memory for a Java object gets created when the object is instantiated. For example private String name = "rocky"; At the end of this statement the memory for the string object name gets created in the memory.
In programming, an "unlimited" string typically refers to a string data type that can grow as needed, often implemented using dynamic memory allocation. The memory space it occupies depends on the length of the string at any given time, along with any overhead for managing the memory (like length metadata). Therefore, there isn't a fixed memory size; it expands as more characters are added, limited only by the system's available memory.
An array of pointers to string would contain, per array position, a number dictating which memory address holds the string data. For example, position [0] in an array would contain a memory address. This memory address can be de-referenced to obtain the actual information held within that memory address.
The strcpy function is declared in the <string.h> header of the C standard library. The function is used to copy a null-terminated string (a null-terminated array of type char). The function accepts two arguments: a pointer to the memory allocation where the copy will be placed; and a pointer to the first character of the null-terminated string to be copied. The memory allocation where the copy will be made must be large enough to accommodate the string, including the null-terminator. Example usage: void f (char* s) { int len; char* c; len = strlen (s) + 1; /* determine length of string (including null-terminator) */ c = malloc (len); /* allocate memory for copy */ strcpy (c, s); /* ... */ free (c); /* release allocation */ }
Memory to memory access is certainly possible in the 8086/8088 microprocessor. Look at the repeated string copy instructions.
A pointer is a reference to a location in memory that is a primitive type in C and other low-level languages. An example use would be if you wanted to store data at run-time, you would have to allocate memory using the malloc function, an you would access that memory area using a pointer Example (modified from http://www.cplusplus.com/reference/clibrary/cstdlib/malloc): #include <stdio.h> #include <stdlib.h> int main () { int i,n; char * buffer; // Declare a pointer to a char printf ("How long do you want the string? "); scanf ("%d", &i); // Get input from user and store in i // Allocate space for i+1 characters buffer = (char*) malloc ( sizeof( char[i+1] ) ); // buffer should now contains a pointer to this location if (buffer==NULL) exit (1); // Memory allocation failed, abort for (n=0; n<i; n++) buffer[n]=rand()%26+'a'; buffer[i]='\0'; printf ("Random string: %s\n",buffer); free (buffer); // Release allocated memory return 0; }
the Pikasso guitar it has 42 strings
immutable means we can't change the content of string object Example:String name = "Tommy"; String fullName = name + "Junior";Here when we declared fullName you would expect it to be appended to the system memory location where the variable name is saved. But in Java it does not happen this way.Here a new variable fullName would be created in memory and the value would be saved there.This is termed as immutable. i.e., String objects cannot be modified in memory once they are created. You may be able to view the change in your program but new String objects would be created in the system memory.Tip:If you want to modify your String objects, then it is advisable to use a StringBuffer instead of a String. StringBuffer allows us to modify string objects and they would be saved in only one place in the system memory. Hence the memory utilization of the system would be much better than the normal String.