C/C++ :: Char Array Keeps Returning Address?

Mar 8, 2015

Code:

charArr = new char[50];
cout << "put in value: ";
cin.getline(charArr, 50);
some_func(charArr);

[Code] ....

Let's say I enter a value: 101

It goes into the if statement but clearly I've enter 1s and 0s. When I debugged, at i = 0, the charArr[i] gives me a value of 49 when assigned to an int variable. But when I cout charArr[i] it gives me 1.

So I'm going to assume 49 is part of the address? How can I correctly check the if statement condition?

View 4 Replies


ADVERTISEMENT

C :: Returning Char Array By Function

Mar 2, 2015

I'm having trouble returning a char array by a function, here's the code. The problem is the 'reverse' function, the purpose of the function is to send two char arrays, 'newline' containing the char array, reverse it and place it in the 'rev' char array then output it back in main, however the output remains blank so I assume there must be something wrong with the reverse function.

Code:
#include <stdio.h>
#define MAXLINE 10
int fgetline(char line[], int maxline);
void copy(char to[], char from[]);
void reverse(char forw[], char rev[], int arrsize);

[Code] .....

View 1 Replies View Related

C++ :: Returning Char Array To Main Function

May 15, 2014

I want to return a char array to the main() function, but its returning garbage value.

#include<stdio.h>
//#include<conio.h>
#include<string.h>
char* strtrmm();
int main() {
char str1[100],c1[100];

[Code] .....

View 5 Replies View Related

C++ :: Wrong Return Type When Returning Char Array As Pointer

Apr 25, 2014

I am writing a class Player which has several char arrays as private fields. I am trying to write a method which returns an array as a pointer, but doesn't alter the array in any way, thus the const.

Here is a snippet:

Code: class Player
{
private:
char state[MAX_STATE_CHAR + ONE_VALUE];
int rating;
char last[MAX_NAME_CHAR + ONE_VALUE];
char first[MAX_NAME_CHAR + ONE_VALUE];
int groupNumber = NEG_ONE;
public:
char * GetFirst() const
{
return first;
}

Visual studio is saying that the return type doesn't match.

View 3 Replies View Related

C++ :: Returning Address Of A Local Variable?

Jul 9, 2014

I know that this code is wrong because it returns the address of local variable which no longer exist after function:

int *f1()
{
int x = 2;
return &x;
}

However, why this one does not show problem and runs fine, here the pointer p is also a local variable:

int *f2()
{
int x = 2;
int *p;
p = &x;
return p;
}

View 6 Replies View Related

C/C++ :: Result Is Returning Address In Memory

Jun 30, 2014

The results of my code is supposed to be very simple: return the 2 integers and then their sum. However, it's doing returning the first value, then an address in memory(rather than the 2nd value), and then the 2nd value(rather than the sum). Here is the code:

#include <stdio.h>
#include <stdlib.h>
struct calculator{
double num1;
double num2;
double result;

[Code] .....

View 4 Replies View Related

C/C++ :: Returning A Char Pointer?

Mar 24, 2015

I am trying to return a char pointer so that i can re use it again. I am writing a vigenere function that takes a message, a key and an initialization vector where it performs the encryption, prints out the encrypted message and returns the encrypted message. I print out the process step by step and everything works, however i pass the answer and print it out again and only the first letter gets changed. I put my code below and my output right after that.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
void decrypt(char *to_encrypt, char *key, char* pct);
enum flag{encryption = 1, decryption = 0};

[code].....

View 3 Replies View Related

C++ :: Pointer (Save Address Value) To Char

Dec 9, 2013

I have a pointer to an Address is there a way to save that address value (not the content but the actual address) into a char ?

View 5 Replies View Related

C++ :: Returning Variables As Char Type With If Statement

Feb 17, 2014

Ive been getting an odd error with this code when I try to compile it, as well as Im not quite sure as how to return my variable "compType" as a char type.

Main
#include <iostream>
#include "Shape.h"
#include <iomanip>
#include <cmath>

using namespace std;
void inputShape( char shape)

[Code] ....

View 1 Replies View Related

C++ :: Store Address Of Customer In Char Variable - How To Read A Line With Spaces

Oct 10, 2014

I want to store the address of a customer (with spaces) in a char variable (say cadd). First I tried to use "cin", as we know it reads until it sees any whitespace. So it reads only first word before a white space. So, I used "getline()" function, it will work. But when I used it, It did'nt wait for the I/P (it skipped it).

char cadd[20];
std::cout<<"Enter Customer Address:
";
std::cin>>cadd;
std::cout<<"Enter Customer Address:
";
std::cin.getline(cadd,20);

View 3 Replies View Related

C :: Fgets To Read A Line Without Returning New Line Char

Nov 5, 2014

I'm using fgets which will read a single line at a time, but unlike fgets I don't want it to return the new line char ( ) ?I want to be able to use printf to display the lines next to each other.

View 7 Replies View Related

C++ :: Comparing Char Array To Char Always Returns True

Dec 23, 2014

I've made a code to check whether or not a save file has been created correctly, but for some reason it always returns this line: readdata[qa]=='1' as true. in which qa is the counter I use in a for loop and readdata is a character array consisting of 50 characters that are either 0, 1 or 2.

this is the entire code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

[Code]....

at first is also went wrong at line 22 and also returned that as true, but then I added brackets and it worked.

View 4 Replies View Related

C++ :: Concatenate Two Char Arrays Into Single Char Array?

Sep 29, 2014

I am trying to concatenate two words from a file together. ex: "joe" "bob" into "joe bob". I have provided my function(s) below. I am somehow obtaining the terminal readout below. I have initialized my memory (I have to use dynamic, dont suggest fixing that). I have set up my char arrays (I HAVE TO USE CHAR ARRAYS (c-style string) DONT SUGGEST STRINGS) I know this is a weird way to do this, but it is academic. I am currently stuck. My file will read in to my tempfName and templName and will concatenate correctly into my tempName, but I am unable to correctly get into my (*playerPtr).name.

/* this is my terminal readout
joe bob
<- nothing is put into (*playerPtr).name, why not?
joe bob joe bob
seg fault*/
/****************************************************************/
//This is here to show my struct/playerInit

[Code]....

View 2 Replies View Related

C/C++ :: Difference In Array Name And Address Of First Elements Of Array

Aug 13, 2014

I have just started learning C language, I have read that array name is the pointer to the first element of array.

So,technically both a and a[0] should have different memory address and a should hold address of a[0],

But when I declare an array and print the address:

int a[5]={2,4,6,8,10};
printf("The address of a and a[0] are &d and %d",&a,&a[0]);
Output is: The address of a and a[0] are 2358832 and 2358832

Why I am getting the same result, shouldn't I be getting different memory address for both a and a[0].

View 2 Replies View Related

C :: Char Array With A Phrase To Char Word

Nov 28, 2013

I need to do a function that copy every word from a text to a char word. How can i do it?

View 5 Replies View Related

C++ :: Returning A String Array?

Jun 15, 2014

I am trying to return a string array but am only getting the first char. I want to return something like this:

8C TS KC 9H 4S

so I can do my proper checks.

Code:
void PokerFile::splitHands(std::string playerHand) {
for (int i=0; i<=13; i++) {
playerOne[i]=playerHand[i];
}
for (int j=14; j<30; j++) {
playerTwo[j]=playerHand[j];

[Code]....

View 4 Replies View Related

C :: Returning Function To Array?

Mar 31, 2013

I need to create a function which will print a list from 100Hz to 1000Hz then 1000Hz to 9000Hz. I have created a function in order to calculate and set up the frequency values from 100Hz to 9000Hz using two for loops as shown below. However I am unsure how to return this to the array at the main.

int main(void) {
double Frequency[18];
system ("PAUSE");
return(0); } double Frequency (void)
{
int count;

[Code]....

View 1 Replies View Related

C++ :: Returning Array From A Function?

Jun 17, 2013

I am trying to neaten my code by putting them in different cpp files but in one function an array is changed:

int function(int array2[100][9])
{
for (int i =0; i < 100; i++)

[Code]....

View 9 Replies View Related

C++ :: Returning Pointer That Points To Array

Mar 5, 2013

I have a program that is trying to find all factors of an integer given. It needs to be done in a recursion function. Right now i have code similar to just getting the prime factors of a integer.

unsigned int * find_factors_using_recursion(unsigned int x ) {
unsigned int * factor = new unsigned int[];//do i put x in here ?
for(unsigned int i = 2; i < x; ++i) {
if(x % i == 0) {
find_factors_using_recursion(x / i);
*factor = (factor[i] = i);
} }
return factor;
delete [] factor;
}

When i cout the *factor = (factor[i] = i) it gives me the prime numbers of the integer passed into the function but when I return the pointer it only returns one of the prime numbers. I'm new to c++, how to return pointers from functions that would be great with an example to go with it.

View 10 Replies View Related

C++ :: Returning Short Int Array In A Function

Feb 14, 2014

I'm trying to return a short int in a function..This is my function signature :

short int* StartRecord(int seconds)
this is my array :
short int waveIn[NUMPTS];

I'm trying to get the data into an array :

short int arr [] = StartRecord(3);

getting this error: Error2error C2440: 'initializing' : cannot convert from 'short *' to 'short []'

View 1 Replies View Related

C++ :: Returning A Pointer To 2D Array From A Function

Dec 10, 2014

I know how to pass a 2-D array to a function. The prototype for that is void f(int (*p)[2]) assuming the array is of integers and there are 2 columns in it.

However, if I wanted the same function to return a pointer to a 2-D array, what would be the prototype?

View 2 Replies View Related

C :: How To Use Two Dimensional Array To Store Address

Apr 11, 2014

I am working on an assignment for class: Create a program that allows a user to enter up to 10 addresses of friends. Use a two dimensional array to store the address of friends. After each address is entered, the user should have the option to enter another address or print out a report that shows each addresses entered thus far. I have created a code that is coming up without errors, but i am not getting the desired results.

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main ()
{
char name[10][10] = {0};
char address[10][10]= {100};
int choice;

[Code]....

My trouble is coming from the the output. I am able to fill the array but i am not able to print my desired results. Where am I losing it in the loop? Also after my first entry if i have space in the "address" input the program prints and ends.

View 6 Replies View Related

C :: Array Function Not Returning Correct Values

Apr 1, 2013

I have been trying to make a function that compares all the values of the array and if they are all equal will return a value to print true. The problem I am having is that regardless of what values I enter the function is always returning true. Any way to tell the program o check all the values in one command instead I put them each,

Code:
#include<stdio.h>
#include<stdlib.h>

int compare(int arrayA[], int arrayB[]);

[Code]....

View 2 Replies View Related

C :: Finding And Returning A Common Value From Array Of 8 Integers

Feb 23, 2013

how assignments is to write code that returns a value that is repeated more than once in an array of 8 integers.For example:

Numbers: 30 12 100 33 2 2 1 40
Output: 2

Here is my code so far:

Code:

#include <stdio.h
int main(void){
int i;
int j;
int arr[8];
int count;
int maxCount = 0;
int maxValue = 0;

[code]....

When I compile this code (on Vim), I get an error when I print the array of 8 above (see ERROR above).

View 3 Replies View Related

C++ :: Returning Dynamically Allocated 2D Array From Function?

Sep 18, 2014

How can I return a dynamically allocated 2d array from a function? Do I use like this:

int main(){
char **array;
array=func();
} char ** func(){
char** ptr=new char[5]; //five words
ptr[0]=new char[size of word1];
*ptr[0]=word1
........
return ptr;
}

View 8 Replies View Related

C++ :: Returning Dynamically Allocated Array From A Function

Jul 27, 2014

This a very simple program I created because I dont understand how do this. My goal is to be able to use the pointer *s5 throughout the program. For example I would to like to call other functions and pass that pointer through the function. I understand the dynamic allocation and pointers for the most part but Im confused here because the "new char[20]" variable will die after the function and I dont want it to.

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
void testArray ( char *s5 );
int main ( int argc, char *argv[] )

[Code] .....

Also does strlen count the null terminator?

View 1 Replies View Related







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