Passing Pointers by reference in C++ Problem


 
Thread Tools Search this Thread
Top Forums Programming Passing Pointers by reference in C++ Problem
# 1  
Old 08-30-2011
Data Passing Pointers by reference in C++ Problem

Hello All,

I am having this issue...where I am actually having hard time understanding the problem:

The code is as follows:

HTML Code:
#include<iostream.h>
 
void fxn(char*** var)
{
int i =4;
*var = (char**)malloc(i*sizeof(char*));
for(int j =0; j<4; j++)
{
*var[j] = "name";
cout<<*var[j];
}
}
 
int main()
{
 
char** var;fxn(&var);
 
}
 
 
Am getting warning at line " *var[j] = "name" ": deprecated conversion from string constant to 'char*''

I know am doing something wrong......bt. can't seem to figure out what!

Thanks n Regards,

Last edited by mind@work; 09-15-2011 at 02:39 PM..
# 2  
Old 08-30-2011
This is a C++ warning about using C, and you will get a lot of them if you will write in a such manner. Don't mix C and C++ styles, idioms and constructs. Either use <stdio.h>, malloc, printf, char*, etc. (so C) or use string, cout, iostream, new, etc. (C++).

===

Ok, I was wrong. But it's a bad style anyway, IMO.

Last edited by yazu; 08-30-2011 at 11:50 AM..
# 3  
Old 08-30-2011
Giving same warning even if I change to total C++ style or vice versa!
# 4  
Old 08-30-2011
yazu: It's nothing to do with using C++ versus C calls. It's perfectly valid to use malloc and cout and new together -- the tricky bit comes figuring out when to call free() and when to call delete(). Smilie

The error means exactly what it says: It's complaining about a conversion from const char * to char *.

What's the only const char * you have? "name".

So:

Code:
#include<iostream.h>
 
void fxn(char*** var)
{
int i =4;
*var = (char**)malloc(i*sizeof(char*));
for(int j =0; j<4; j++)
{
*var[j] = (char *)"name";
cout<<*var[j];
}
}
 
int main()
{
 
char** var;fxn(&var);
 
}

Just be careful not to free() or try to modify "name" later.
# 5  
Old 08-30-2011
@Corona688

If I typecast constant string "name" to (char*)"name"....it gives me "Segmentation Fault"!!!!
# 6  
Old 08-30-2011
It's getting the order of operations mixed up, adding an offset to a pointer then dereferencing it instead of dereferencing it then adding an offset. (*var)[j] instead of *var[j]
This User Gave Thanks to Corona688 For This Post:
# 7  
Old 08-30-2011
@Corona688

Thank you ....even when I added bracket so that it dereferences properly ...got perfect answer!!!!!!
Thanks ton Smilie

---------- Post updated at 11:38 AM ---------- Previous update was at 11:36 AM ----------

@yazu:
Thanks ....will surely try to implement good styling Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell Scripting Problem - Invalid Back Reference

Here is the question... Create a new script, sub2, taking three parameters... 1.) the string to be replaced 2.) the string with which to replace it 3.) the name of the file in which to make the substitution ...that treats the string to be replaced as plain text instead of as a regular... (1 Reply)
Discussion started by: johnhisenburg
1 Replies

2. Programming

Passing by reference question

Hello All, I have a question in the following sample code: If I am passing a reference of a variable, will the dynamic allocation happen in which it is passed? Will the dynamic allocation will actually change the memory contents of char array "arr"? int main() { char *arr; ... (1 Reply)
Discussion started by: mind@work
1 Replies

3. Homework & Coursework Questions

Passing pointers to struct

Hi, i'm trying to copy a struct into a binary file using the unix instruction write, so i declare and fill the struct "superbloque" in one function "initSB" and then i pass the pointer to another function called bwrite (for block write) which calls write. The problem is that i call the function... (2 Replies)
Discussion started by: ignatius3
2 Replies

4. Shell Programming and Scripting

Replace character based on reference file problem asking

I got two files right now, input file (target file), reference file 1 (query file) reference file 1 (long list of data) KOLOPWMOPOPO ADASDASD ADSASDASDAD . . target file (one long liner content) ADASDASDTYUKOKOLOPWMOPOPOOPLUAADSASDASDADPOPOUYADADASDASD desired output file content ... (1 Reply)
Discussion started by: patrick87
1 Replies

5. Programming

Problem With Pointers

Hi guys. What is the difference between these: 1. int *a; 2. int (*a); (2 Replies)
Discussion started by: majid.merkava
2 Replies

6. Shell Programming and Scripting

BASH: File name part to list reference problem.

I've made a habit of including a four-letter "tail" on image file names I download from the Web, so I can both match them with IPTC Transmission References of my own making and rename them later using either a GUI renamer or a script I've written myself. Now I want to automate the process of... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

7. Shell Programming and Scripting

Array reference problem

i have a variable MYHOST that has my host name.depending on the host i have an array like A_<hostname>.Everytime i need to append the hostname to A_ to get the array.but in the shell script i am nt able to access the members of that array. code of what i hav done: export temp=A_$MYHOST for... (15 Replies)
Discussion started by: niteesh_!7
15 Replies

8. Programming

Problem with array of pointers

Hi All, I am using the array of pointers and storing the address of string.This is a global list. So i am using extern to give the reference of this list to another file and using reading the data from this string. But list is being corrupted and string is missing some characters in... (2 Replies)
Discussion started by: lovevijay03
2 Replies

9. Programming

Need a simple example of passing FILE pointers

Dear friends, can anybody pls tell me how to pass FILE pointer in c. I am so confused .. :confused: suppose I ve two function 1. file_open() 2. read_line() I want to call these function from main() function and in file_open() function it will open that file and in read_line()... (5 Replies)
Discussion started by: user_prady
5 Replies

10. Programming

Problem with pointers, structures, and Pthread

Hello all, I'm working on a small wrapper library for a bigger project, and i've been killing my self over (what I think is) a pointer problem. Here is the code (I extracted the part of the code where the problem is for better reading, I tested the code below, and I get the same problem):... (13 Replies)
Discussion started by: tmp0
13 Replies
Login or Register to Ask a Question