C :: Getting Null Pointers In Most Of Functions

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


ADVERTISEMENT

C++ :: Pointers To Automatically Null When Object Is Deleted

May 18, 2013

Say I have an object and 10 pointers to it in several other objects of varying class types. if the object gets deleted, those pointers have to be set to null. normally I would interconnect the object's class with the classes which have pointers to it so that it can notify them it is being deleted, and they can set their pointers to null. but this also has the burden that the classes must also notify the object when THEY are deleted since the object will need a pointer to them as well. That way the object doesn't call dereference a dangling pointer when it destructs and attempts to notify the others.

Auto pointers and shared pointers are not what I'm looking for - auto pointers delete their object when they destruct, and shared pointers do the same when no more shared pointers are pointing to it. What I'm looking for is a slick method for setting all pointers to an object to null when the object destructs.

View 1 Replies View Related

C :: Passing Pointers To Functions

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

C :: Pointers As Arguments In Functions

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

C/C++ :: Pointers Passed Through Functions

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

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 View Related

C :: Functions Which Change Arrays Without Pointers?

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

C++ :: Calling Functions - Vector Of Pointers

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

C :: Using Typedef Struct And Passing Pointers To Functions

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

C/C++ :: Assigning Functions To Array Of Function Pointers

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

C++ :: Null Terminator Same As Null And Through False Value?

Feb 18, 2014

I am looking at one of the functions of an exercise:

void escape(char * s, char * t) {
int i, j;
i = j = 0;
while ( t[i] ) {
/* Translate the special character, if we have one */
switch( t[i] ) {

[code]...

Notice the while loop evaluates the current value in t to true or false. When we hit the null terminator, does that get evaluated as 0 and hence evaluates as a falsy value so the while loop exits?

View 1 Replies View Related

C# :: Session Is Null But Not Null?

Mar 31, 2015

When you login to my site my loginservice which is done by ajax and json make a session called context.Session["Name"]. With BreakPoints it shows that everything is good and the variables are in place. However when I use Session["Name"] it comes out as null.

I will add my code at the bottem not

using System;
using System.Collections.Generic;
using System.Linq;

[Code].....

View 2 Replies View Related

C :: Create Array Of Pointers To Pointers Which Will Point To Array Of Pointers

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

C++ :: Comparing Char Pointers To Integer Pointers

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

C/C++ :: How To Access Linked List Functions From Stack Class Without Functions

Mar 20, 2014

I'm a little confused by my programming assignment this week. I've been working at it Wednesday and I've made progress but I'm still confused as to how I'm supposed to do this. The class I made is called Stack, and it's derived from a template class called StackADT. We also utilize a class called unorderedLinkedList, which is derived from a class called linkedList.

We're supposed to implement all of the virtual functions from stackADT in the Stack class. The Stack data is stored in a an unorderedLinkedList, so what I'm confused by is how to implement a few of the Stack functions because there are no functions in unorderedLinkedList which we could call to manipulate the data.

As you can see from my attached code, I'm really confused by how I'm supposed to implement the pop() and top() functions, and I also think my initializeList() function is wrong. We don't have any similar functions in unorderedLinkedList to call, so I'm at a loss of how i'd access my unorderedLinkedList. My initial thought was to call the similar functions in the class that unorderedLinkedList was derived from, linkedList, but I'm unsure of this is what we're supposed to do, or if theres actually a way to access my unorderedLinkedList without having to use the functions from the base class.

NOTE: We're not allowed to modify stackADT, unorderedLinkedList, and linkedList.

Stack.h

#include "stackADT.h"
#include "unorderedLinkedList.h"
template<class Type>
class Stack: public stackADT<Type>{
template <class T>
struct nodeType
{
T info;
nodeType<T> *link;

[Code]...

View 3 Replies View Related

C/C++ :: Array Of Functions Pointing To In Class Functions With Arduino

May 3, 2013

At the moment im trying out with pointing to an array of functions. I got this working as following:

typedef void (* functionPtr) ();  
functionPtr functions[2][2]={{do11,do12}, {do21,do22}};    
void do11(){DEBUG_PRINTLN("11");}
void do12(){DEBUG_PRINTLN("12");}
void do21(){DEBUG_PRINTLN("21");}
void do22(){DEBUG_PRINTLN("22");}    
void loop(){
         A=0;
         B=1;
         functions[A][b]();
}  

But now I'm trying to use this to point to a function inside a class so instead of do11, i want to be able to point to Basic.Do11. Somehow this doesnt work and I keep on getting this message:

error: argument of type 'void (Basic::)()' does not match 'void (*)()'

View 2 Replies View Related

C++ :: Pointing To NULL And 0 The Same Thing?

Apr 13, 2014

Is:

char *endp = NULL;

the same thing as:

char *endp = 0;

?

View 1 Replies View Related

C++ :: Using STRTOK With Null Fields?

Jul 23, 2013

I am beginner in C++ programming. And i was try use STRTOK code with NULL fields, but in ARRAY fields NULL values is skiped. For example:

input text in Memo2:

AnsiString PAT02[100][20];
for (int IndexVRadku02 = 0; IndexVRadku02 < Memo2->Lines->Count ; IndexVRadku02++) {
AnsiString PNF02 = Memo2->Lines->Strings[IndexVRadku02];
char * PN02;

[Code] ....

Result:

Array 00 01 02 03 04 05
00 0000 TEXT1 TEXT2
01 0002 TEXT3 TEXT4 TEXT5
02 0003 TEXT6

But i need this result:

Array 00 01 02 03 04 05
00 0000 TEXT1 TEXT2
01 0002 TEXT3 TEXT4 TEXT5
02 0003 TEXT6

View 2 Replies View Related

C++ :: Point Of NULL In CString

Jul 9, 2014

I dont see any point of NULL in cstring. The code given below just outputs same as it would have done with NULL. My understanding is if size of char array is less than length of char array then null must be manually added?

#include <iostream>
using namespace std;
int main(){
char chr[0];
cin>>chr;//or if you use cin.getline;
cout<<chr<<endl;
return 0;
}

Enter something: hellowwwww
hellowwwww
Segmentation fault (core dumped)

why? for NULL char or something else?

View 1 Replies View Related

C++ :: Pointer As Parameter Is Null?

Apr 9, 2014

In my raytracer-project I have the following function:

bool hitObjects(Ray &ray, std::vector<Object *> &objects, Vector3& normal, Material *mat) {
double tmin = K_HUGE_VALUE;
double t;

[Code]....

When I try to run this, I only get the message "Material pointer is null". So, somehow the pointer is null after the hitObjects-function is called, even though it isn't inside that function.

View 6 Replies View Related

C/C++ :: MySQL - Resultset Is Always Null

Jul 30, 2014

The resultset is always null and the connection is alive. The VS2013 debugger says that mysqlcppconn.dll has no debugging symbols. Tested the query on a mysql console and it worked fine, it returned 1.

CDatabase::CDatabase(CSettings* settings) {
settings = settings;
try {
Connection* con = get_driver_instance()->connect("", "", "");
con->setSchema("hyperbot");
cout << "Connected." << endl << endl;

[Code] .....

I also get these strange warnings on compile:

1>c:program files (x86)mysqlmysql connector c++ 1.1.3includecppconnsqlstring.h(38):
warning C4251: 'sql::SQLString::realStr' : class 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
needs to have dll-interface to be used by clients of class 'sql::SQLString'

[Code] ......

View 10 Replies View Related

C/C++ :: Strtok And Null Checking?

Mar 1, 2015

I'm playing around with parts of code and am coming across some errors. Most of my concern is related to strtok(). I've used it before but with a char* named token. I used a while loop to continuously check whether token was equal to NULL. In the following code, however, there aren't any checks. I was wondering if that is why this code prints (null) while running. Also, I would like to know if it is possible to read input like this code attempts to do - assigning tokens to each variable one after the other.

The format of the input:

Zucchini, Squash, pound
Yellow, Squash, pound
Tomatoes, Ugly Ripe, each
#include <stdio.h>
#include <stdlib.h>

[code]....

View 3 Replies View Related

C/C++ :: Initializing Pointer To NULL?

May 17, 2014

So I'm writing a small program for class, and for some reason I keep getting an error when trying to initialize head to NULL. Even threw in the namespace just to see, nothin'.

#ifndef NUMBERLIST_H
#define NUMBERLIST_H
using namespace std;

[Code].....

There's my header file. Not sure what I'm doing wrong with the constructor.

EDIT: Got it to work with nullptr, but still curious why that isn't working

View 2 Replies View Related

C# :: Linq To SQL With Null Value In WHERE Clause

Apr 13, 2015

I am trying to query fields in a where clause using LINQ to SQL and for some reason I cannot figure out why it doesn't work:

var qryGetMonsterID = (from students in dbContext.tblStudentPersonals
where (students.givenname.Equals(fn)) && (students.familyname.Equals(ln)) && (students.middlename.Equals(mn)) && (students.email.Equals(e))
select students);

The returned SQL Syntax is:

SELECT [t0].[monsterid], [t0].[givenname], [t0].[middlename], [t0].[familyname], [t0].[homeaddress], [t0].[city], [t0].[state], [t0].[postal], [t0].[primaryphone], [t0].[secondaryphone], [t0].[email], [t0].[username], [t0].[lastmodified], [t0].[modifiedby]
FROM [dbo].[tblStudentPersonal] AS [t0]
WHERE ([t0].[givenname] = @p0) AND ([t0].[familyname] = @p1) AND ([t0].[middlename] = @p2) AND ([t0].[email] = @p3)

Not sure but the values for middle name could be null - because not everyone has a middle name and the same is true for e-mail.

View 6 Replies View Related

C# :: Need To Replace Null Value By 0 To Calculate

Jun 28, 2014

I have a datagridview (dgv) that is bound to database:

QtyOnHand QtyOnHold QtySold QtyAvailable
10 2 1 null
12 null null null
null null null null
7 5 null null
25 null 5 null

I want to iterate through the datagridview and calculate the last column with this formula:

QtyAvailable = QtyOnHand - QtyOnHold - QtySold

so the datagridview should become:

QtyOnHand QtyOnHold QtySold QtyAvailable
10 2 1 7
12 null null 12
null null null null
7 5 null 2
25 null 5 20

I use this code:

foreach (DataGridViewRow row in dgv.Rows){
if (string.IsNullOrEmpty(row.Cells["QtyOnHand"].Value.ToString())){
row.Cells["QtyAvailable"].Value =
Convert.ToInt32(row.Cells["QtyOnHand"].Value) -
Convert.ToInt32(row.Cells["QtyOnHold"].Value) -
Convert.ToInt32(row.Cells["QtySold"].Value) -
Convert.ToInt32(row.Cells["QtyAvailable"].Value);
}
}

The problem is when any of the value on the right side of the equation is null, the whole equation is invalid. I need to replace the null values by 0 on the fly so that it can be calculated. Note that I do not want to actually replace the null values in the second and third columns of the table, just replace it in the equation whenever it applies to make the calculation work.

View 4 Replies View Related

C++ :: Create Null Pointer After Using Delete

Aug 7, 2014

In jumping into C++ it says something like this: It's not necessary but when you delete a pointer it's a good idea to reset it as a null pointer. That if your code try's to dereference the pointer after being freed, your program will crash. This happens to a lot of experienced programmers.

This could corrupt users data. delete p_int;
p_int = NULL;

1. If you can deference a pointer after the memory is freed, why can't you just delete the pointer?

2. If you can do 1, how do you delete the pointer using code?

3. Every thing I've read says that free memory is handed out in a sequenced order. I don't believe that is true at all. I may be wrong. Why can't you put the data in any number of places if it will fit. Isn't the compiler smart enough to know where bytes (bits)and pieces are stored?

4. If you storing anything in free memory must use a pointer to it?

5. Can a pointer or something similar be used with stack memory?

View 13 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved