Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
VOID change(CHAR *TP, CHAR *FR, BOOL Vw)
{
CHAR *User, *NU, *CU = NULL, *Ref = NULL;
int Loop = 0;
<Some lines of code>
if(Loop == 0)
memcpy(Ref, FR, strlen(FR)+1);
CU = NU;
Loop++;
}
if(CU)
free(CU);
}// end of function
Ok so as you can see... the FR is a pointer to a character array.. and each time it sends the memory location..
and I have to copy it to Ref..
But all these are pointer variables..
What I need to do is.. I need to make a character array Lets say
AZ and it will contain,
AZ = the character array FR points to + "/abcd" +
then i will do a memcpy of AZ to Ref...
My problem is the FR variable is a pointer and I cant understand how to put that into a character array and then add characters to the final array..
Make sure that Ref has enough memory allocated to hold the strings that you want to concatenate.
Then you can use the strcpy() function to copy the first string to Ref, and strcat() to concatenate additional strings. For example:
strcpy(Ref, AZ);
strcat(Ref, "/abcd");
strcat(Ref, more_stuff);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I'm trying to make a login/register project and I have difficulties in declaring the char* tempUsername from this code (SIGSEVG segmentation fault)
char *tempUsername, *tempPassword, *tempPasswordConfirm, *tempSecurityQuestion;
/*
no other declaration for tempUsername here
*/
std::cout<<"Enter your new username:\n";
std::cin>>tempUsername;
//process stops here
if(fileSearch(newFilename(tempUsername))) {
std::cout<<"Username already exists! Choose another username!\n";
}
else {
std::cout<<"Enter your password:\n";
std::cin>>tempPassword;
std::cout<<"Confirm your password:\n";
I'm having a hard time understanding anything about pointers, so any advice is more than helpful!
char *tempUsername
std::cin>>tempUsername;
The problem here is that your pointer is uninitialised. When you try to extract from the input stream into the uninitialised pointer, the behaviour of the program will be undefined. Don't do this.
Your goal seems to be to read a string of user input. A solution that I can recommend is to use the std::string class:
std::string tempUsername;
std::cin >> tempUsername;
No need to use a pointer to achieve this.
I can use a char* as an array of chars, is that true?
It is not true in general. If you have a char* that points to an element of an array of char, then you can use that char* as an iterator to access the elements of the array.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am new to c++ programming and I am currently working on my school project that involves manipulating data stored as a const string array that contains five strings.
Part of the requirements of this project is that I need to create an array of pointers that hold the data stored in the const string array and then create a student object for each of the "students" stored in that array to populate the array of pointers.
I have so far been experimenting with vectors to create the array of pointers, but have consistently hit a brick wall with trying to piece it all together. If someone could give some insight, I would really appreciate it. Thank you.
So, basically you want to create an Array of pointers, where each pointer points to the first char of a string.
The solution to these kind of problems can be dealt with using the code below:
#include<stdio.h>
#include<string.h>
int main()
{
char *fruits[] = {
"apple",
"mango",
"orange",
"bananas",
"grapes"
};
for(i = 0; i < 5; i++)
{
printf("String = %10s", fruits[i] );
printf("Address of string's first char = %u\n", fruits[i]);
}
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need store in a uint8_t pointer determinate positions of array.
Example:
uint8_t array[40] = {1,2,3,etc...}
uint8_t *pointer = array[5] - [25] . Save in pointer only positions between 5 to 25. I need in C++ language.
Thanks Community!
// Indices for the desired range of values.
constexpr std::size_t first = 5;
constexpr std::size_t last = 15;
// Create a new array with the appropriate size.
uint8_t array2[last - first];
// Copy the data to the array.
std::copy(array + first, array + last, std::begin(array2));
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a buffer that will sent the pointer to read the data directly to do zero copy. But how can I protect the address pointer from code outside the class?
const void * returnReadPointer(size_t arraySize)
{
if ( arraySize < MemoryUsageInArray)
{return array + arrayIndex}
else{ return null}
}
The return pointer can access the array which is the buffer. I want to try to protect it from improper usage.
If you hand the address of a memory location to your client, they can do anything with it. Change your API and instead of returning an address, provide only the functionality they will need:
<your_type> readValue(size_t index)
{
// index validation etc.
.
.
return array[index];
}
Make the return value a const void *.
const void * returnReadPointer(size_t arraySize)
You can change the function prototype as below to protect your pointer from being changed.
const void * const returnReadPointer(size_t arraySize)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to modify a c string in C++.
void modify(char* s)
{
s[0] = 'a';
}
If I do this, there will be some undefined behavior and can't run.
Let's assume s[0] is valid. I know char* s is immutable. Is there any possibility that I can modify the s[0] in place, which means, without creating a new string. Do the modification on the original string.
I think you might be misunderstanding some other answers you have seen on the web.
It is only undefined behavior to modify a string constant, not any char*.
As long as you strdup the constant string into a non-constant string, you can make whatever changes you want with it, because it is now in a mutable region of memory.
#include <stdio.h>
#include <string.h>
void modString(char* changeMe) {
changeMe[0] = 'g';
}
int main(){
char* foo = strdup("food");
puts(foo);
modString(foo);
puts(foo);
free(foo);
}