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.
I'm given a mathematical function F(x) = etc..., the user inputs an initial x point and a final x point. The program finds the integration.
Below is a snippet of code.
/*Typedefs as given by prof*/ typedef double (*mainFunction) (double); typedef double (*calcArea) (mainFunction, double, double); int main () { answer = calcIntegral(mainFunction *curve1, calcArea *calcAreaRect, a, B)/>; /*it doesn't like this line*/ printf("The integral from %lf to %lf is: %.4lf", &a,&b,&answer); return 0; }
curve1 is a function that accepts a double and returns a double, calcAreaRect takes mainFunction main(which is F(x) so i stored the fn in curve1), double and double.
I am working on an assignment identical to another post from a couple years ago, for reference here is the thread:
array of pointers to structures sorting addresses by zip code
They way it is written on that thread is almost identical to the way the teacher implied to have it done (only wrote part of the input block). But I am having an error:
When it gets to the output section it outputs then next name along with the zip code... I tried strncpy and strxfrm but both cause more problems than they did work.
The last part of the project is to have the output put out in order of least zip code to most zip code (00000<99999), so this is causing me a real problem and I do not see what exactly is making this happen.
Here is my code (we dont HAVE to use gets but professor suggested using it for this assignment, next lab is to rewrite this using files and fgets rather than I/O redirection):
I have not started the sorting code because I cannot get past this, but once I have proper zip codes I am sure I can make a sort function no problem.
I am using xcode with some breaks to read variables as various points and do not notice anything wrong until it makes it to the output functions, although this page briefly pops up between input and output functions when the breaks are up:
I am trying to pass a struct through an array to another array from one microprocessor to another microprocessor and then dereference that array to update the same exact struct on the other microprocessor.My goal is to always have values inside of both structs while sending any updates to the struct on the other microprocessor in the mean time but I seem to be looping through the data and the members of the struct seem to be cleared out after every clock cycle. I can verify this by the flashing of my leds which the struct controls.
What I'm trying to do with this code is an address book and I have an array of pointers which are returned by malloc whenever I need to add an extra entry in the address book. What I need to do is search for a specific entry using bsearch. I've got an inqSort() function that sorts the table and runs normally, but my program crashes when I try to use bsearch.
Every time an entry is inserted, I inqsort() the array so it's always sorted, and it works as expected. But when I try to call findEntryUI(); from the main() function, the program crashes after entering the name I want to search.
I am trying to pass multiple args to g_signal_connect through a struct and it's members. I think the problem lies with execlp. I am trying to pass the struct members to it but I keep getting errors. No matter how I format execlp and the struct args, I always get an error. The current error I receive is Bad address. Like I have said before, no matter how I format execlp, I receive some sort of error.
You should be able to enter those fields, and after some time when while loop is stopped (when u press n) it should print out that PhoneBook bellow. I don't understand what is the problem but I think it is that Display function.
I'm quite new to C and these days I have been playing around with a linked list. I managed to make a working version using pointers, ad only for the sake of learning I was trying to do the same thing just passing the "Object reference" Here is the method that apparently doesn't work..
Code:
struct Node addNode(struct Node head){ int value; struct Node *n; printf("Please enter the value "); scanf("%d", &value);
[Code]...
when I return the function i have something like: head=addNode(head)
Unfortunately it does not work the way I aspect. I suppose that there is something I have left out..
Code: like n->next=&head // passing the address of the head at the next pointer of the struct head =*n //copy the values of the new node to the old head..
There must be something wrong with this line.. return head; What have I done wrong?
I have a linked list where each node contains a pointer to a string, I'm treating the linked list as a text file and each node/string contains each line in the text file.
I have all of that working, but now I need to create a switchLine function that takes in the two line numbers and I need to switch those lines in the text file. I initially thought of switching the pointers to each the next node in the linked list such that the node order changes, but I then realized how much work that would actually take when you need to consider so many cases. So I instead thought of changing the pointers to strings. If I want to switch the 2nd and 3rd line, then I'd want to have the 2nd node in the list have its char * pointer point to the memory position that the 3rd node's char pointer points to, and vice versa.
I have a vector (structures) in a struct (instances). I make a declaration of this struct called instance. The vector is a 3-layer vector of pointers, like so:
vector < vector < vector<scene::IAnimatedMeshSceneNode*> > > structures; (The type is from Irrlicht 3D). I have 3 nested "for" loops which looks similar to the following:
for (int a = 0; a < instance.structures.size(); a++) { /*note:vector size previously set*/ for (int b = 0; b < instance.structures[a].size(); b++){ for (int c = 0; c < instance.structures[a][b].size(); c++) {
if (1) { //checking value of variable not included in snippet
These are currently referencing the pointers, it seems. The program compiles but crashes at this point. I need them to reference the values of the pointers. Problem is, I don't know where to put the dereference operator (*). Where should it go?
Code: //prototype void improvedbubblesort (struct team Table[]); //this is defined out side of main
[Code]....
the whole code is very long but can post if necessary . Basically i get no errors in this but its not sorting them . Think its in the way i pass the array of struct .
I am working on incorporating a function in to an already existing piece of code, I have incorporated the function fine as far as I am aware.
The problem I have is that I am trying to pass two int arrays to the function, so that i can manipulate and compare them "the values will be changed the originals cannot be changed"
I am having trouble pulling the information out of the already created array, I am able to pass the pointer reference for the single value which is not exactly what i want "best_prog".
My function is below I have commented the memcpy parts and also the majority of the code isn't there cause it is not needed to see make the copy work.
int edit_distance(int index) { struct prog *progp = &population[best_prog]; /* The struct of best prog not sure if i need one for the other prog I am trying to compare it with the one below doesn't work as intended.*/ //struct prog *progp = &population[]; int editdistance = 0, ar1 = 0, ar2 = 0, a = 0, b = 0, j = 0, x = 0;
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.
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.
forget everything from before. It came out of confusion regard the supplier functions. dis_s() and read_s() the part functions work and are not any different really.
when I run the dis_s() function it just prints out garbage until it segments. starts with a bunch of 0 and newlines until it starts printing locations on my computer...... it worked on campus with debian and I only copied it from gmail to my computer so I really don't understand whats up(im on mint).
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?
//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);
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++) {