C/C++ :: Stuck On Using Pointers With Malloc
Apr 18, 2015
So this is my first attempt at actually writing code, I have a little basic core functionality set up and I'm 99% sure I'm doing something very fundamentally wrong with pointers.
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
struct Creature {
int pow;
int tou;
[code] ....
Error generated:
magic.c: In function "main":
magic.c:71:26: warning: initialization from incompatible pointer type [enabled by default]
struct Creaturedb *db = Initialize_creaturedb;
^
magic.c:72:22: warning: initialization from incompatible pointer type [enabled by default]
struct Player *pl = Initialize_player;
^
View 3 Replies
ADVERTISEMENT
Jan 8, 2014
I'm trying to keep track of the size of blocks of memory that a pointer points to. No matter what I do, this code below always outputs the integer 8.
If I change 1000 to 5, I still get 8. If I change it to 0, I get 8... If I change it to -1, I get 8. If I change int *a to double *a, I get 8. If I take away the & symbol, I get 8. If I use *& instead, I get 8.
Why? I want it to output 1000. If I change that to 500, I want it to output 500.
int *a;
a = malloc(1000 * sizeof(int));
int j = sizeof(&a);
printf("%d", j);
I want to build my skills where I can allocate, inspect and change memory sizes.
View 4 Replies
View Related
Mar 30, 2013
I am able to work with n instances of a structure in one mallocated area, but when I try to do the same thing with just character pointers, I get compiler errors about making integer from pointer without a cast. If I create a structure with just a character pointer in it, it works just fine... I am just not seeing something here!!!
This works:
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
struct items {
unsigned int item_1;
unsigned int item_2;
[Code]...
This DOES NOT work!
Code:
#include <stdio.h>
#include <stdlib.h>
int main (void) {
char * items_ptr = NULL;
unsignedint i = 0;
char * one = "one";
char * two = "two";
[Code]...
View 9 Replies
View Related
Mar 2, 2015
so my program is quite simple and it works fine when i execute it but when i try to run through it with gdb i get an error. so this is what i get from gdb
Code:
Breakpoint 1, main () at fun.c:4
4 {
(gdb) watch a
Hardware watchpoint 2: a
(gdb) next
6 a=0;b=0;c=0;
(gdb) next
[code]....
View 6 Replies
View Related
Mar 27, 2013
Alright I have a program that needs to calculate different rates of commission for a particular sale
I'm stuck in a loop and I'm not sure how to update the final cout statements as well.
#include <iostream>
#include <cmath>
using namespace std;
[Code].....
and yes those are the actual rates i need to calculate and they are in increments of 5%
View 2 Replies
View Related
Feb 15, 2015
how to ask for the input (className) without getting stuck in the loop. When I remove
char className;
cout << "Enter the class name: " << endl;
cin >> className;
it works fine. When I leave it how it's displayed below it goes in an infinite loop. By the way I think "char" is not the appropriate way to declare className. If I want to output it later, should I changed it to "string"?
#include <iostream>
using namespace std;
int main()
{
[Code].....
View 2 Replies
View Related
Feb 24, 2015
my code seems to enter an infinite loop should a user mistakenly enter a character other than a digit. The code is about selecting from a preset number of options, numbered 1- 4. If the user mistakenly enters a letter for example, instead of a number, the infinite loop kicks in...
View 1 Replies
View Related
Apr 4, 2013
I have a school project where I have to create a class with 2 attributes that default to 1. To verify that they are at 1, I have a cout before any user inputs or value manipulation. It works for the most part. What happens is that if I change the defaults to 17, the cout will retain the old values until I have an unsuccessful build (remove a semicolon from the end of the line). When I have a successful build (put that semicolon back), the cout displays the changed values. Change the values back to 1 and the same thing happens again until I have an unsuccessful build then a successful build. Are the old values getting stuck in memory somewhere?
// Rectangle.h
// Chapter 9_Problem 9.11_Page 412
#include <iostream>
using namespace std;
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle // Rectangle class {
public:
Rectangle(double = 17, double = 17); // default constructor setting values to 1
[code]....
View 10 Replies
View Related
Feb 7, 2015
I want to create a loop that that generates a random number set and gives the user the option to run the loop again for a new number. I have that working but when the loop ends the variable value does not leave the loop. I tried a posttest and pretest loop.
Is it possible to declare/create/alter a variable in a loop and have that value exit the loop to use in the code later? I am still learning about global variables.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
int oneDiceSix() // Roll 1d6
{int oneDiceSix = 1+(rand()%6);
return oneDiceSix;} //end function
[code]....
View 2 Replies
View Related
Jul 18, 2013
Code:
#include<stdlib.h>#include<stdio.h>
#include<unistd.h>
#include<math.h>
int main(void)
{
double N, NG, LG, epsilon, root; // setting all variables to type double
[Code] .....
The goal is to create a program to calculate the square root of a number provided by the user to an error tolerance 0.005
Looking around i fond the Code: fabs(NG - LG) < epsilon); section that was very similar to what i was using, but if this is better im down for that.
My issue, from what i can see, is the updating of the values of LG. if the test for error tolerance fails then LG needs to take on the value of the results of NG.
Now I am not 100% that is the point of failure due to the fact the script does calculate the root properly, but it never exits the program once it reaches the tolerance level.
Code: imac:ENG-3211 user$ ./hw_4_1
Please enter the number you wish to find the square root: 4
4.00 2.500
4.00 2.050
4.00 2.001
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000
4.00 2.000
And it continues forever until i manually break the program even though the answer was found, in this example, on the 4th loop.
View 8 Replies
View Related
Sep 9, 2012
I currently have a hangman game in the making. Which is giving me debugging issues when I go to pick a letter, it will keep asking for a letter, if I place a break; within the loop it asks for a letter and says you've won. I know all I should need is a couple extra lines somewhere within the code.
/// Play game
public static string playGame() {
Words words = new Words();
[Code].....
View 2 Replies
View Related
Mar 2, 2013
in order to calculate some logarithm approximation I need to make a variable, say s, search through the whole integer numbers.
Code:
int s;
for {;;s++){
}
[Code] ....
since the variable needs to be initialized right? how to go through all the numbers?
View 3 Replies
View Related
Oct 29, 2014
I've written some code to randomly generate probabilities:
int Queue::car_per_hr() {
srand( time( NULL ));
int prob = rand() % + 101; // Probability in percentage from 0 to 100
int cars; //no of cars per hr
[Code] ....
So it checks through the range of possibilities: 0-20%, 21-40% etc. I then have this to implement it:
#include <iostream>
#include <ctime>
#include "queue.h"
using namespace std;
int main(){
Queue car_wash;
int NO_HOURS;
[Code] .....
For some reason though, everytime I call the car_per_hr() function, the probability just stays the same. It's output is always locked to the same thing it had to begin with?
View 3 Replies
View Related
Feb 25, 2014
Here is my code and everything works except i can not get the highest array number to be output. it always says 10.
My instructions are:
(1) Create a 10-integer array called data
(2) Set a pointer ptr to point to array data
(3) Output the elements in array data using pointer ptr
(4) Find the largest element in the array data using pointer ptr
#include <iostream>
#include <cstdlib>
using namespace std;
[Code].....
View 4 Replies
View Related
Aug 28, 2014
Working on a project for a c++ class and got a bit stuck.
What I am trying to do is include a class for calculating home square footage in a program I have written for my last project. When I do this, I get errors in codeblocks, on lines 74 and 87, "expected ; before 'box', and 'box' was not declared, respectively. Unsure what my snafu is... I had these two programs separate and both run without errors until I combine the two into one program.
View 2 Replies
View Related
Mar 18, 2014
I'm having trouble getting my loop to work correctly. If I iterate the for loop once it works as expected and displays proper output. When I try to iterate two times or more the program gets stuck in an infinite loop.
testData8.dat:
Code:
12 9
13 756
View 3 Replies
View Related
Oct 21, 2013
I am making a math game program that simple and need to have a user input a name and then that name.txt is brought up and use variables from the text to add to their score. heres my code kinda long.
#include <iostream>
#include <cmath>
#include <cctype>
#include <string>
#include <ctime>
#include <cstdlib>
[Code] ....
View 10 Replies
View Related
May 10, 2014
I am making a MFC application with VC++2012 with receiving data from Serial Port and the data is stored in a Listbox, then after processing (convert into numbers) and get these data to draw a sine wave on the chart. (I intend to make a virtual oscilloscope).
Everything works ok at the beginning, but after receiving about 300 items or above (in the Listbox) the value seem stucking (or lagging) and work very slowly.
I receive data by the SerialPort_DataReceived() method, and process of converting and drawing chart also in this method, I think this makes the system overload!!
View 1 Replies
View Related
Jan 1, 2013
The program I have is from a tutorial where the user enters two points on a line, and then the program calculates the mid-point and slope.
I want to modify it from it's initial form so that co-ordinates can be input in (x,y) fashion. Right now the user has to enter the x-coordinate, enter a space, and then enter the y-coordinate (lame...)
I saw people using SStream and using that to either write functions to ignore the comma or similar things for converting one file into an array, but not quite what I am trying here.
// program to determine slope of a line
#include <ios>
#include <iostream>
#include <istream>
#include <limits>
using namespace std;
void myflush ( istream& in ) {
[Code] .....
View 2 Replies
View Related
Mar 7, 2013
I am trying to get this code eventually to read in a maze file to move the smiley face around in. But right now my current snag is the yes or no to enter the for loop.
#include <iostream>
#include <windows.h>
#include <conio.h>
#include <time.h>
using namespace std;
int main() {
int name;
char ans;
[Code] .....
View 3 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
Nov 2, 2013
I wrote a program to detect if a graph is tree or not. Initially it was using static memory. Later I changed it to use memory dynamically using malloc().My problem is that, my program it works great for case when graph is not tree but fails if it is.
Code:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int main()
}
[code]....
View 2 Replies
View Related
Mar 3, 2015
This is my code without malloc. I need to change the array size so there is no max size for each matrix. I must dynamically allocate space for all arrays used. So I need to use malloc to create my arrays. So I cant use int A[rows][cols].
Code:
/* This program asks the user for 2 matrices called A and B, as integers, and displays their sum, C. The max dimension of each matrix is 100. */
#include<stdio.h>
// Construct function
void construct()
{
int m, n, i, j;// Variables
int first[100][100], second[100][100], sum[100][100];// Matrices variables
[Code]...
Im having a hard time understanding/using malloc. Should first, second, and sum be single pointers or double? How do I scan the matrix correctly? And how do I add them properly? Do I have to change my for loops?
Code:
/* This program asks the user for 2 matrices called A and B, as integers, and displays their sum, C. The max dimension of each matrix is 100. */
#include <stdio.h>
#include <stdlib.h>
// Construct function
void construct()
[Code]...
View 3 Replies
View Related
Jan 6, 2014
I am confused whether 'malloc'ing an array of pointers and pointer to a group of arrays are the same.
Suppose I need an integer pointer to a group of arrays of 2 elements each.
int (*LR)[2];
So to dynamically allocate this pointer for M number of arrays suppose, then what is the correct syntax?
int M;
scanf("%d",&M);
int (*LR)[2];
LR = (int*)malloc(2*sizeof(int));
Whether this is correct??
View 14 Replies
View Related
Oct 27, 2013
I am curious as to what is happening to my pointers and everything once I call malloc to create space for 5 integers to try to make a stack.
Code:
#include <stdio.h>
#include <stdlib.h>
void add(int * TOP, int * stack);
int main() {
int *stack = NULL;
int *TOP = NULL;
stack = (int *)malloc (5 * sizeof(int));
[Code] ....
I am guessing that when I initialize stack to malloc, stack now stores the starting address of where the space is taken out, and also assigns TOP that address too. I get the choice from the user (b) to get the instruction to try to push on the stack. I was told that the if statement in the function checks if the stack has passed the bounds of 5 elements. If not, it assigns the scanned variable and puts it into what TOP is pointing to and increments TOP to the next address. It is not working and am wanting to see where my logic is wrong.
View 4 Replies
View Related