C :: Structs - Implement In With Pointers And Functions
Feb 23, 2013
I have a struct and I want to implement in with pointers and functions.
What is the corect syntax? For example:
Code:
typedef struct XYZ
{
int x;
int y;
int z;
}XYZ_t;
int func( using the XYZ_t struct)
[Code] .....
View 5 Replies
ADVERTISEMENT
Dec 14, 2014
What the second print statement prints
main() {
struct s1{
char *str;
int i;
struct s1 *ptr;
[Code] .....
I executed the code and the output was some number(for second printf) but it should print a string.
View 3 Replies
View Related
Feb 14, 2013
I have an assignment where I need to use pointers to do a few things and I am a little confused on the syntax of it all. My question is how do you use a pointer to point to an array of structs.
For example
struct info{
char firstName[15];
char lastName[15];
};
main() {
info person[4];
cout << "The third letter of the second persons first name is: "; // ?????
}
how would I define a pointer to point to the third letter of first name the second person in the "person" array.
View 2 Replies
View Related
Apr 19, 2014
I have this struct declaration to create a linked list
struct node {
string y;
node* next;
};
If I create a linked list of 3 nodes
A->B->C->NULL
A->y is "a"
B->y is "b"
C->y is "c"
Since they are consecutive in memory. How far apart are the strings in memory?
I tried doing something like
int n = &(head->next->x)-&(head->x);
The problem is that I got 6 while debugging and 2 otherwise.
View 1 Replies
View Related
Feb 24, 2013
Background: I'm writing a convolutional encoder (and decoder, eventually) for a microprocessor (PIC24), for which I'm using structs and pointers to move from state to state. So far as I'm aware, everything I'm using in the PIC involves nothing other than ANSI C.
I have a little experience with structures, having written a linked-list program for a class a couple years back, but nothing since and never used structure arrays. I have the feeling I'm missing something basic here, which is what's so frustrating. The most confusing error (and I suspect the root of most of them) is the 'state undeclared', which I just can't figure.
The errors I'm getting are:
encoder.c:11: warning: 'struct memstate' declared inside parameter list
encoder.c:11: warning: its scope is only this definition or declaration, which is probably not what you want
encoder.c: In function 'state_init':
encoder.c:22: error: two or more data types in declaration specifiers
encoder.c:25: error: 'state' undeclared (first use in this function)
encoder.c:25: error: (Each undeclared identifier is reported only once
[Code]....
Code:
Code: //Includes
#include <stdlib.h>
//------------------------------------------------------------------------------
//Creates state machine and passes back pointer to 00 state
void state_init(struct memstate* startpoint)
{
extern struct memstate
{
char output0; //output if next input is 0
[code]...
NB: I'm aware that at the moment, this code will do nothing except spin round that do-while loop. Once it's actually compiling I'll drop in some simple button-based test code so it'll check for the correct output.
View 9 Replies
View Related
Mar 8, 2013
I think i am getting confused with passing structs and functions all in the same...When I run through the program (it compiles), the functions that add coins do not add, but rather just replace an old value with a new one.
#include <cstdlib>
#include <iostream>
using namespace std;
struct coinbox {
[code].....
View 1 Replies
View Related
Mar 11, 2014
write a program which contains two global variables:
1.Account number (integer)
2.Account Balance (float)
and three functions :
A function called set values which sets initial values for account number and account balance,
A function called set values which prompt user to enter the values of the above variables,
A function called input transaction which reads a character value for transaction type that is D (for deposit) and W (for withdrawal ) and a floating point value for transaction amount which updates the account balance .
Implement a pointer to call each of the functions using C++.
View 1 Replies
View Related
Nov 8, 2014
How to implement the main queue functions Enque() and Deque() using two stacks S1 & S2. You allowed only to call push () and pop() functions of the two stacks to implement Enque()and Deque() functions.
#include<iostream>
using namespace std;
const int size=5;
int arr[size];
int front=-1,rear=-1;
bool isfull() {
if(rear==size-1)
[Code] .....
View 4 Replies
View Related
Aug 17, 2013
I am supposed to implement the member functions of class Person.
class Person {
public:
Person();
Person(string pname, int page);
void get_name() const;
void get_age() const;
[Code] ....
The code I wrote is below. Where I am struggling is the program does not allow me to input age. Therefore, I cannot test if my temp for age works. It automatically defaults to 0 because it hasn't taken input. Here is my code:
// Program Title: Person function
// Program Description: The program prompts the user for first and last name and age.
// It then prints the output that was provided by the user.
#include<iostream>
#include<string>
using namespace std;
class Person {
[Code] .....
View 13 Replies
View Related
Dec 3, 2014
I need to write a widget that will implement some matrix functions using data from a text file. The input data will be spreadsheet like and fully determined with complete rows and columns. I need to do the following,
1. populate a data structure with the input data (double) to create matrix x
2. transpose the input data matrix x to create x'
3. multiply x * x' to create a square matrix x'x
4. take the determinant of x'x
This is pretty standard linear algebra, but not something I have done in cpp before. Implementation such as the best data types to use to store each of the three matrices, how they are sized, and what library functions may be available for the matrix functions like transpose, multiply, determinant, etc.
I will be removing each data row from the initial input to observe the effect on the determinant in case that has any effect on program design.
View 5 Replies
View Related
Sep 19, 2013
I got an assignment at school asking for a program that can implement functions that store, get and deletes text/binary data to a given memory area. We are supposed to make kind of like a tiny-mini OS..any links to some tutorials or explanations hon ow to understand this and be able to make a program like this on my own?
View 9 Replies
View Related
Apr 15, 2013
I am having a lot of trouble being able to get data from a file and input it into given structs and arrays of structs and then outputting the file. We are given a file that contains 96 lines and looks like this:
Arzin, Neil
2.3 6.0 5.0 6.7 7.8 5.6 8.9 7.6
Babbage, Charles
2.3 5.6 6.5 7.6 8.7 7.8 5.4 4.5
This file continues for 24 different people and then repeats with different scores (the second line).
The first number, in this case is 2.3 for both people is a difficulty rating. The next 6 numbers are scores.
We are given this data in order to set up our structs and arrays and my code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main () {
ifstream inFile;
inFile.open("C://diveData.txt);
[Code] .....
View 1 Replies
View Related
Feb 28, 2015
I have been struggling with pointers. I am trying to write a program that first asks a user to input a filename. It then checks if the file exists and if it does it passes a pointer to the next function. The next function then asks the user for a specific word to look for and the function will search a text file for the word and do some other operations. My problem is that I do not understand how to use the pointer returned by my first function as an input to another function.
The following code has the first function file_check() and the second function word_search() which I think the way I am declaring it is the problem.
Code:
FILE *file_check();
void word_search(FILE *);
int
main(void) {
FILE* check= file_check();
// word_search(check);
[Code] ......
View 6 Replies
View Related
Aug 23, 2013
I'm having some problems with a function. The function is supposed to find the two largest values in an array.
Code:
void find_two_largest( const int *a, int n, int *largest, int *second_largest){
largest = a;
int temp;
second_largest = a;
for ( int i = 1; i < n; i++){
if (*(a + i) > *largest){
temp = *largest;
[Code]....
I don't see any mistake with the code of the function, but when I try to call it inside my program it only returns 0 for both largest and second_largest.
Code:
int *find_middle( int *a, int n);
void find_two_largest(const int *a, int n, int *largest, int *second_largest);
int main()
{
int n;
[Code]...
Do I have to declare the variables largest and second_largest as normal integer variables and then pass their addresses as arguments to find_largest or is that incorrect?
View 10 Replies
View Related
Oct 15, 2014
For some reason I keep getting null pointers in most of my functions, and in my convertTime function the numbers are way higher than they should be.
Code:
//Utils.H header file----------------------------------------------
//This function takes the radius of a circle and returns the diameter, the circumference and the area.
int circleStatistics(double radius, double *diameter, double *circumference, double *area);
//This function takes a number of days and returns how many years, weeks and remaining days that is.
int convertTime(int days, int *y, int *w, int *d);
//This function takes a length of time and calculates the dilation of that time at a percentage of the speed of light.
int lorentzTimeDilation(double normalTime, double percentC, double *dilatedTime);
[Code] ....
View 3 Replies
View Related
Dec 14, 2014
I am writing a code that creates a deck of cards using a doubly linked list. One function, newDeck(), is made to create a new deck if the user wants. I don't have any problems creating the deck and it all seems to work fine, but when I run the whole program and a separate function needs to utilize the nodes in my deck, I get an error. Trying to find where there was a problem, I tried displaying the values of a card node in random parts of my code. At the end of the newDeck function, after the whole deck has been created, the card still displays correctly, yet when I return back to main immediately after newDeck has been called, I noticed that the values of my cards changed to either random values or they became null. I found this strange because in between the end of my newDeck function and at this point, there is no extra code so it doesn't seem as if there is any way the pointers could have been changed. Maybe It's because I don't have a complete understanding of pointers yet but is there any way that pointers can change values when returning from a function to main?
newDeck(head, tail, n); //this is how I call the function from main.
void newDeck(Card* head, Card* tail, Card* n) //this is my function body
{
for (int i = 0; i < 4; i++)
{
[Code]....
View 2 Replies
View Related
Jan 17, 2014
the book I learn from gave a task to write a program which gets a matrix , and we have to write a function that switches 2 columns or rows the user inputs .as far as I know a function can not change variables in the main function without using pointers .so , theoretically, can a function described here can be written without using pointers ? as far as I tried - it can not.
View 9 Replies
View Related
Mar 27, 2013
I have a vector of pointers inside a seperate Exam class.
vector <Question* > question_list
The Question class is my base class in which I have derived sub classes for the different types of questions (MultipleChoice, LongAnswer, etc.). I am using my vector to hold the different types of questions.
in each of those classes I have virtual "write" functions in both the base and the derived classes, that write to a file differing for each type of question.
My problem now is calling the write function from a Exam function. I've tried several methods, such as:
for (size_t i = 0; i < question_list.size(); i++) {
question_list[i].write(testfile.c_str());
}
but it comes with two errors: "error C2228:left of '.write' must have class/struct/union" along with "IntelliSense: expression must have class type"
I have made a write function for the exam class as well but am not sure what it should include since the Exam class is not a derived class of the Question class.
View 15 Replies
View Related
Mar 3, 2013
I am having problems with passing my values through functions.
Here is my code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void test(int**);
void test2(int**);
[Code]....
View 8 Replies
View Related
May 13, 2013
I'm trying to create an array of function pointers and then assign compartilbe functions to them, so I can just call *pf[0](xxx);
The functions are all of the type
void func01(unsigned char*, int, int)
how would I create an array of function pointers and assign the address of the functions to them? So I could call them like
ptrToFunction[i](charBuffer, 10, 20);
I've read a bit on line and I thought I could do it but so far I've failed.
It seems trivial and I feel I'm close but close isn't good enough.
I'd like to assign the fuction addresses like this:
for (int i=0; i<10; i++)
if (i==1)
ptrToFunction[i]=func01;
if (i==2)
ptrToFunction[i]=func02;
etc.
The actual logic is somewhat different than this but this close.
View 4 Replies
View Related
Feb 28, 2014
I'm trying to create an array of pointers to pointers which will point to array of pointers (to strings) I tried
Code:
int i;
char *string[]={
"my name is dave",
"we like to dance together",
"sunny day",
"hello",
[code]...
the app keeps crashing , I don't know how to make the array-elements to point to another array-elements..
View 4 Replies
View Related
May 21, 2013
I am a little confused while comparing char pointers to integer pointers. Here is the problem:
Consider the following statement;
char *ptr = "Hello";
char cArr[] = "Hello";
When I do cout << ptr; it prints Hello, same is the case with the statement
cout << cArr;
As ptr and cArr are pointers, they should print addresses rather than contents, but if I have an interger array i.e.
int iArr[] = {1, 2, 3};
If I cout << iArr; it displays the expected result(i.e. prints address) but pointers to character array while outputting doesn't show the address but shows the contents, Why??
View 2 Replies
View Related
Jul 30, 2014
If i want to input (FROM A FUNCTION) information into a struct:
* How do I define/ call/ prototype that function.
* How do I check if that array inside the function is empty?
View 1 Replies
View Related
Mar 12, 2014
I'm working on a project involving nested classes and structs like this:
Code: class A {
public:class B {
public:f()
{A::C* iCanDoThis; //no errors.
iCanAlsoDoThis->root->.... //this also works fine.}private:A::C* iCannotDoThis //this is what I would like to do.
Has errors
A* iCanAlsoDoThis;};private:struct C
{..data..};
C* root;};
Is it possible make a pointer to struct C a private member of class B?
View 1 Replies
View Related
Apr 19, 2014
The main is not working properly, I get strange missing parenthesis notices and array errors which I am not understanding. How to identify the errors. Functions are included.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int menu ();
double change_value (double * value);
[Code] ......
View 3 Replies
View Related
Jul 20, 2014
I'm currently working on a program that writes an array of struct to a file and then read back the data from the file to another array of struct. At the bottom is an image of my result.
My goal is to end up with two identical struct arrays but my program fails to do this. My struct have to members: ID and kind (of animals in this case). I declare my first arraystruct africa[] with "monkey" and "giraffe" with their respectively IDnr: 112 and 555. I stream this data to a file and read read them back to the arraystruct get_animal[]. Simply I want the get_animal[] to be identical with the africa[] when the program is over, but that is not so. According to my result(bottom image) it display:
112, monkey (get_animal[0])
112, monkey (get_animal[1])
meaning that get_animal[0] is identical to africa[0] get_animal[1] is also identical to africa[0]
but why? I want get_animal[1] to be identical with africa[1]. meaning I want the result to look like this:
112, monkey
555, giraffe
I've also made the program to print the parameters of my fwrite/fread calls. Why is the 3rd parameter = 1 meaning that only 1 element will be read/written when my program just read/write 2 elements?
Code:
main(){
FILE *fp;
struct animals
{
int id;
char kind[20];
} africa[1] = {{112,"monkey"}, {555,"giraffe"}};
}
[code]....
View 3 Replies
View Related