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.
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 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
How do I correctly ask for object rock in main. I simplified the code, figuring this was the only problem. The error "expected primary-expression before '&' token.
void createObject(vector <object>& obj, world wld)
{
....
}
int main()
{
object rock;
createObject(vector<object>& rock, level_1);
return 0;
}
Very simply:
int main()
{
std::vector<object> rock_vector(1);
createObject(rock_vector, level_1);
}
You can't pass rock to it, as it's not a vector. You need to pass an actual vector to it. Here, I made rock_vector of size 1, so it's at least got one object in it (so rock_vector[0] is more or less your replacement for rock).
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'd like to write a function that will return 4 members of a class. They are 3 ints and char, and I'd like to store them all in one vector and return it from a function call. Can I do that?
You either need an std::tuple if you want to preserve the types and if the result length is constant, or just cast all members to some common supertype and store them in a container.
You need a class:
struct S
{
int a, b, c;
char letter;
};
int main()
{
S s;
}
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
For example:
string str[3];
void foo(char** str)
{
//do something to str...
}
How to pass str[] to function foo in a convenient way?
The function expects an array of pointers, so you'll have to make one from your array of strings:
std::vector<char*> pointers;
for (auto & s : str) {
pointers.push_back(&s[0]);
}
foo(&pointers[0]);
Beware that this may not be valid if the function modifies the pointers, or the strings they point to. A better option would be to avoid mixing C and C++ style string handling, if possible.