C++ :: Passing Data Structures To Function Pointers And Get Values
Sep 26, 2014
typedef struct example_dt_struct {
int a;
float b;
char* c;
};
typedef void(*func)(example_dt_struct *s, int e, int f);
void f(func *n){}
how can i use example_dt_structure with my function pointer into f()
View 5 Replies
ADVERTISEMENT
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
Feb 26, 2014
I am bit confused in passing the structure to a function. I made a small code and it doesn't build !
Code:
#include<stdio.h>
struct Compute_off_Time(struct WHATTIME);
typedef union _Time {
unsigned long int Value;
[Code] ....
View 1 Replies
View Related
May 7, 2013
I am looking for an example of when passing arrays to other data that has pointers,
I understand the terms I would just like to see a small example of code that actually demonstrates this process.
View 4 Replies
View Related
Dec 3, 2013
I want to scan numbers in from within a function, but have access to them in main, so I tried using pointers to do so:
Code:
// Path Of Exile socket colours simulation
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
[Code].....
View 9 Replies
View Related
Mar 4, 2013
If f1 and f2 are two user defined functions.
main(){
int *p;
f1(&p)
}
f1(**p){
f2(&p);//
If I've to pass the pointer by reference again in another function, will I've to do something like this?
}
f2(***p){
**p = NULL;
}
View 3 Replies
View Related
Nov 21, 2014
I have a a group of text files that are used as input into a program. Another very similar program needs the same data in a different input format. I am writing a program that will read in each line of data and parse it. I have successfully written the program in main() such that it will read until the end of file. However, I am trying to re-write the program such that the algorithm is in an external function and will read one line at a time and then pass all of the character strings to the main program. The program will not compile and I am positive it has to do with an incorrect use of arrays in passing the variable "token". I am attaching a copy of the main program and the function.
#include <iostream>
#include <fstream>
#include <cstring>
#include <stdio.h>
void Line_Parse(std::ifstream&,std::ostream&,const char* token);
int main(int argc, const char * argv[]) {
[Code] .....
View 2 Replies
View Related
Jul 6, 2013
The program asks for to ask the user for how many students there are, and how many strawberries they picked. The farm gets to keep half and the students get to keep the rest.
I am using functions and passing values to one function to another. I know I am making it harder then I need to, but I want to work with functions more and passing values. I figured it was better to practice with simple programs.
Code: #include <stdio.h>
float people(float);
float strawberries(float x);
[Code].....
View 4 Replies
View Related
Apr 29, 2014
I'm working through the Let us C book. One exercise asks me to collect int and float , then pass them to a function that gets product of these and returns it to main. My code looks like this:
Code:
#include <stdio.h>#include <stdlib.h>
main()
{
int a;
float b, c;
[Code]...
So while compiling i get an error about conflicting types for product. I tried to google that error but couldn't understand what's the problem. My only clue is that i can't pass int and float to a function at the same time... Could that be it?
View 6 Replies
View Related
Jan 28, 2014
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <time.h>
#include<iomanip>
#include<array>
#include <algorithm>
using namespace std;
const int AS = 6;
void FillingRandomly(int (*)[AS]);
void printing(int (*)[AS]);
[Code] ....
Basically I have to create an array, fill it, and then print it on screen. The tricky thing is that need to use pointers to fill it and print and later on sort it. My problem is that with this code is that i get
Error2error C2109: subscript requires array or pointer typec:userspcdesktopusbanthonydocumentsvisual studio 2012projectsessaieessaieessaie.cpp55
and
5IntelliSense: expression must have pointer-to-object typec:UserspcDesktopUSBAnthonyDocumentsVisual Studio 2012ProjectsEssaieEssaieEssaie.cpp55
Whenever I try to run it.
View 2 Replies
View Related
May 9, 2014
This read a file (arrival time , brust time) for n processes and suppose to do shortest job first scheduling. the file read correctly but passing the value to SJF function gives read of 0's to all values .
#include<stdio.h>
#include<stdlib.h>
typedef struct {
int brust_time[10];
int arrival_time[10];
}Time;
[Code] .....
View 1 Replies
View Related
Feb 3, 2015
I have the main() - which has entries like
Code:
main() {
int ss, ret, z;
float yy;
ret = function1(&yy, &ss, &z);
//int function1(flat *cc, int *dd, int *k) - is it correct?
[Code]...
View 5 Replies
View Related
Oct 11, 2013
I am trying to read the file which has the values stored in the following pattern
2 4 10 103 2 504 .... and so on
I Have opened the file and then passed the opened file to another function to process it further.
here is my code
#include <iostream>
#include <fstream>
#include <vector>
#include <stdlib.h>
[Code].....
now the problem is when the control exits the "while loop" in the function "readingValues(std::ifstream& myFile)it goes straight to catch block ?
View 9 Replies
View Related
Mar 11, 2013
I am new to C++ ... My question is if I were to pass some data or values from the main() function through a void function (which main purpose is to display the data in a certain manner); how should one go about doing so?
For instance:
Code:
// example.cpp -- poorly written, just trying to learn
#include <iostream>
void format() {
using namespace std;
cout << "Time: " << min << hrs << endl;
[Code] .....
Obviously this is really poorly written, and confusing for the user. My main goal is to learn how to pass through values to a void function.
Also a bit off-topic, but is there a downside to place "using namespace std;" outside a function say if out of 100 functions only 10 of them use it? Would it make the program slower/unstable in any way or is this something one could do without any downsides?
View 3 Replies
View Related
Aug 9, 2013
How do I store pointers to a struct in an array ? I am using sprintf to concatenate some values together and then output it to an array in its 1st argument. A portion of my code is shown below.
Code:
int count = 0;
struct addx {
int a;
[Code].....
View 1 Replies
View Related
Mar 24, 2015
concepts on pointers to structures and referencing for the following two lines.
//address of the variable "struct UIP_IP_BUF" is assigned as srcipaddr
uip_ds6_nbr_add(&UIP_IP_BUF->srcipaddr,
//data type "uip_lladdr_t" pointer points to the address of array an "nd6_opt_llao" with size UIP_ND6_OPT_DATA_OFFSET
(uip_lladdr_t *)&nd6_opt_llao[UIP_ND6_OPT_DATA_OFFSET]
View 5 Replies
View Related
Feb 13, 2013
This is my program and i dont know what is the better strategy to display the output perfectly align with the title, when i input a long variable or short the variable move and it does not align with its title. what can i do.
#include <iostream>
#include <string>
#include <string.h>
#include <cstdlib>
#include <cstring>
#include <iomanip>
using namespace std;
struct book {
[Code] ....
View 1 Replies
View Related
May 6, 2014
Write a program that uses a record structure to store a Student Name, Student ID, Test Scores, Average Test Score, and Grade. The program should keep a list of test scores for a group of 6 students. The program should ask for the name, ID, and four test scores for each student. Then the average test score should be calculated and stored. The course grade should be based on the following scale:
Average Test Score Course Grade
------------------ ------------
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
59 or below F
A table should be displayed on the screen listing each student's name, ID, average test score, and course grade. Implement with functions.
My code runs but it isnt returning anything (readable/correct) and i have no clue why. This is what i have so far:
#include <iostream>
using namespace std;
const int columns = 4;
struct StuRec //user defined datatype {
int id[6];
char names[6][20];
[Code] .....
it compiles completely but at the end instead of showing the student name ID average score and class grade it shows.... [URL] .....
View 2 Replies
View Related
May 5, 2014
Write a program that uses a record structure to store a Student Name, Student ID, Test Scores, Average Test Score, and Grade. The program should keep a list of test scores for a group of 6 students. The program should ask for the name, ID, and four test scores for each student. Then the average test score should be calculated and stored. The course grade should be based on the following scale:
Average Test Score Course Grade
------------------ ------------
90 - 100 A
80 - 89 B
70 - 79 C
60 - 69 D
59 or below F
A table should be displayed on the screen listing each student's name, ID, average test score, and course grade. Implement with functions.
My code runs but it isnt returning anything(readable/correct) and i have no clue why. This is what i have so far:
#include <iostream>
using namespace std;
const int columns = 4;
struct StuRec //user defined datatype {
int id[6];
char names[6][20];
int scores[6][4];
double avg[6];
char grade[6];
}; //semi-colon required
[Code] ...
View 8 Replies
View Related
Nov 1, 2014
I wanted to print the values of a array from a function by passing the array as well as the number of elements to be read. For a single dimensional array, this is how i have written it. It's pretty straight forward. I want to read 5 elements from the 5th element in the array.
Code:
#include<stdio.h>
void display(int array[],int size) {
int i;
[Code]....
With this code I want to print the five elements from the element present in [0][4].
But shows an error that
Code:
D:BennetCodeblocks CLearning CSingleDimentionalArray.c||In function 'main':|
D:BennetCodeblocks CLearning CSingleDimentionalArray.c|18|warning: passing argument 1 of 'display' from incompatible pointer type [enabled by default]|
D:BennetCodeblocks CLearning CSingleDimentionalArray.c|2|note: expected 'int (*)[10]' but argument is of type 'int *'|
||=== Build finished: 0 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|
I know when you pass a array as an argument it gets decomposed into a pointer, but with a multi-dimensional array this is not the case. how this works for mult- dimensional array's?
View 3 Replies
View Related
Dec 14, 2014
I'm trying to learn structure type in C. In this trying, the code must take 9 value from user but it takes 6 values then returned. I scrutinized but I can't find my error.
#include <stdio.h>
typedef struct books {
char name;
float price;
int pages;
} book;
[code].....
View 2 Replies
View Related
Mar 5, 2012
Write a program that accepts three values (int) from the keyboard into three variables a, b and c. After loading the variables, your program will arrange the values into a, b, and c, so that a contains the larger, be the next and c the smallest.
You can use any other variables in addition to a, b c, as you see fit.
You will have to use some variation(s) of the if construct.
There are many ways to program this, but your challenge will be to achieve this without doing all possible comparisons. In fact, three comparisons woudl be enough, if you , for example, determine the largest number first, and keep track of its position in some way , so you can compare the other two.
I think I might use a code similar to the one below.
switch (expression) {
case constant1:
group of statements 1;
break;
case constant2:
[Code] ....
View 14 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
Jul 2, 2014
I created one structure and the created structure is using in few more structures, but i did not get the proper data.
typedef struct _MSG_HEADER{
//MESSAGE_HEADER 40 bytes
short iApiTcode;
short iApiFuncId;
int LogTime;
char AlphaChar[2];
[Code]...
My requirement is have to fill the MessageHeader structure & we can use that header in all the structures entire program.
View 3 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
Dec 4, 2014
I'm taking a programming class and currently we're doing data structures. Today, we discussed how to save square matrices column-wise with data structures. The teacher said that after the first few steps where you declare the structure, allocate the matrix, free it and get the dimension (which mostly make sense to me I think), you have to "get" and "set" the matrix by putting in something like
Code:
void setMatrixEntry(Matrix* A, int i, int j, double Aij) {
A->entries[i+j*A->m] = Aij;
}
[Code]....
View 7 Replies
View Related