C++ Array of Pointers from Array of strings [closed] - c++

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;
}

Related

Declaring a char pointer [closed]

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.

is a star followed by a class name defining an array? c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I programmed several years ago, in which I wrote everything in the main function instead of using classes and other functions so I'm pretty confused. I have a project that has a specified format, and the way the array is defined is confusing me. I initially thought that xArray was a dynamic array, but when I try to assign a size to it, it gives error E0513 "a value of type "int *" cannot be assigned to an entity of type Group1
Group1{
int x,y;
}
Group2{
int a,b;
Group1* xArray;
}
void main{
Group1 g1;
int length;
cout <<"enter the length"<< endl;
cin >> length;
g1.xArray = new int[length];
}
so basically, I have a few questions, 1. what exactly is xArray(pointer? array?) 2. if it is a dynamic array, can I assign a length to it (which would be an input variable) outside the class where it's defined?
Your issue is that you are attempting to assign an array of one type, to a pointer of another. Here is a simpler example:
struct OneInt {
int a;
}
...
OneInt *o = new int[5]; // This cannot work, int is not the same type as OneInt
OneInt *o = new OneInt[5]; // This will allocate memory for an array of type OneInt
But honestly, you should do neither. You are looking for a dynamically sized vector, so c++ helps you by providing std::vector:
Group2{
int a,b;
std::vector<Group1> xArray; // By using an array, the memory is initialized and
// handled automatically
}
Group2 g;
g.xArray.resize(5);
This way, we don't need to mess around with new and delete and we have better, exception safe, encapsulated code. This is a technique of modern c++ called Resource Aquisition is Initialization (RAII).

How to push strings into a stack? [closed]

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 6 years ago.
Improve this question
I want to push strings like red, blue, and green into a stack
//This is my structure containing the stack and top pointer
typedef struct{
char stk[10];
int top;
}STACK;
//This is my push funtion
void push(STACK stak, char str[])
{
stak->top++;
strcpy(stak->stk[stak->top], str);
return;
}
I want to form a stack like so
red
blue
green
Am I doing it right?
For the basics the answer to your question is definition of stack itself.
A stack is a basic data structure that can be logically thought as linear structure represented by a real physical stack or pile, a structure where insertion and deletion of items takes place at one end called top of the stack.
What you are doing is creating a stack of chars, and trying to push string to it.
Instead you should create a stack of strings.
typedef struct{
string stk[10];
int top;
}STACK;
void push(top,string str)
{
top++;
//overflow condition here
strcpy(STACK.stk[top],str);
}
Also a lot of things are different in C and C++, so please decide first which language you gonna stick with. That will help you get better answers.

copy a character array from a pointer variable [closed]

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);

C++ How to resize a dynamic array without losing data? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to resize a dynamic array without losing data.
Like this:
double * pVecDin;//POINTER
int num_values = 2;
pVecDin = new double[num_values];
pVecDin[0]=5;
pVecDin[1]=6;
int new_num_values=4;
pVecDin = new double[new_num_values];
//Next I lost value of pVecDin[0] and pVecDin[1]
pVecDin[2]=8;
pVecDin[3]=9;
Do I need make an Auxiliar Dynamic Array to copy the old values?
Like:
int new_num_values=4;
double * pVecDin_aux; //POINTER
pVecDin_aux = new double[new_num_values];
pVecDin_aux = pVecDin;
for(int i=0; i < n; i++)
{
pVecDin_aux[i] = pVecDin[i];
}
Make a new, empty array with the new desired size.
Copy everything in the old array to the new array.
Delete the old array.
Assign the pointer of the old array to the address of the new array.
Or use a vector.
As nhgrif has mentioned, you need to make copy of the array.
There is another way if you can use malloc for memory allocation, then you can use realloc for resizing the array.
There are better ways as suggested by other like to use std::vector or list.