14 Replies - 2245 Views - Last Post: 11 June 2011 - 11:54 PM Rate Topic: -----

#1 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

using char* as reference variable and read from a function

Posted 11 June 2011 - 01:42 AM

Hi,
I want to read a string (char *). i.e, I want to send char* argument
and after calling the function I want the argument to be filled in that function definition.
i.e, allocation of memory and assiging value is done in that function definition.
And if i print the value of that in main function i shd get it.

Ex:
void fun(char* receivedString)
{
//here i need to allocate memory and assign value to the receivedString
//so that it reflects in main() function
}
int main()
{
  char *strToBeAssignedInFun;
  fun(strToBeAssignedInFun);
  printf("string is : %s", strToBeAssignedInFun);
}



I have tried allocating memory using malloc and assigned some string but if i run its giving
Segmentation fault.
I am in need of it immediately if you know please help out

Is This A Good Question/Topic? 0
  • +

Replies To: using char* as reference variable and read from a function

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 01:53 AM

Please show us your best effort so far.
Was This Post Helpful? 0
  • +
  • -

#3 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1620
  • View blog
  • Posts: 3,078
  • Joined: 30-May 10

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 01:59 AM

If you had this, would you know how to fix it by using a pointer to an int?
void fun(int var)
{
  var = 42;
}
int main()
{
  int value = 0;
  fun(value);
  printf("value is : %d", value);
}


Was This Post Helpful? 0
  • +
  • -

#4 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 02:14 AM

View PostSalem_c, on 11 June 2011 - 01:59 AM, said:

If you had this, would you know how to fix it by using a pointer to an int?
void fun(int var)
{
  var = 42;
}
int main()
{
  int value = 0;
  fun(value);
  printf("value is : %d", value);
}



Yeah I know about other types but little confused with char * (and string).
void fun(int &var)
{
  var = 42;
}
int main()
{
  int value = 0;
  fun(value);
  printf("value is : %d", value);
}



or

void fun(int *var)
{
  *var = 42;
}
int main()
{
  int value = 0;
  fun(&value);
  printf("value is : %d", value);
}



And one more thing actually my function definition is in one cpp file and declation is in
difference file and directory.

View Postjanotte, on 11 June 2011 - 01:53 AM, said:

Please show us your best effort so far.


Since I am implementing for my official project not able to
post code so only typed some example code.
I have allocated memory in function definition and assigned some characters but its giving segmentation fault.

Thanks in advance
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1620
  • View blog
  • Posts: 3,078
  • Joined: 30-May 10

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 02:33 AM

> Yeah I know about other types but little confused with char * (and string).
Well it's exactly the same.
Just put the *'s or &'s in where you know where to put them for an int.
Was This Post Helpful? 0
  • +
  • -

#6 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 02:33 AM

Expand your example to do the stuff that is causing you trouble.
Take what you have shown us and add the malloc() call and some kind of string work.
Compile it and run it and confirm your example produces the same error you are concerned about.
Once you have that example, share it with us.
Was This Post Helpful? 0
  • +
  • -

#7 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 02:49 AM

Well it's almost exactly the same as with int, just that you need to allocate an array of elements. Are you sure you're not writing more characters than bytes of memory you allocated? Are you sure you terminated your c-string with '\0' character?

Did you already try using debugger to step trough the suspicious code and see what happens?

This post has been edited by Xupicor: 11 June 2011 - 02:51 AM

Was This Post Helpful? 0
  • +
  • -

#8 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 09:32 AM

View Postjanotte, on 11 June 2011 - 02:33 AM, said:

Expand your example to do the stuff that is causing you trouble.
Take what you have shown us and add the malloc() call and some kind of string work.
Compile it and run it and confirm your example produces the same error you are concerned about.
Once you have that example, share it with us.

file4.cpp
void fun2(string &s)
{
   s = "defined";
}

file3.cpp
void fun1(char* receivedString)
{
   string str;
   fun2(str);
   strcpy(receivedString, str.c_str());
}

file2.c
void fun(char* receivedString)
{
   fun1(receivedString)
}

file1.c
int main()
{
   char *strToBeAssignedInFun;
   fun(strToBeAssignedInFun);
   printf("string is : %s", strToBeAssignedInFun);
}



This is what I am doing exactly and not working... Please help me out..
Was This Post Helpful? 0
  • +
  • -

#9 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 09:45 AM

View PostXupicor, on 11 June 2011 - 02:49 AM, said:

Well it's almost exactly the same as with int, just that you need to allocate an array of elements. Are you sure you're not writing more characters than bytes of memory you allocated? Are you sure you terminated your c-string with '\0' character?

Did you already try using debugger to step trough the suspicious code and see what happens?

I have also tried allocating memory using malloc()
file4.cpp
void fun2(string &s)
{
   s = "defined";
}

file3.cpp
void fun1(char* receivedString)
{
   string str;
   fun2(str);
   char* temp = str.c_str();
   receivedString = (char *) malloc(sizeof(char) * str.size()+1);
   for(int i=0; i<str.size; i++,receivedString++ temp++)
      *receivedString = temp;
   *receivedString = '\0';
}

file2.c
void fun(char* receivedString)
{
   fun1(receivedString)
}

file1.c
int main()
{
   char *strToBeAssignedInFun;
   fun(strToBeAssignedInFun);
   printf("string is : %s", strToBeAssignedInFun);
}



I know this is not correct way but tried this also :rolleyes2:

View PostXupicor, on 11 June 2011 - 02:49 AM, said:

Well it's almost exactly the same as with int, just that you need to allocate an array of elements. Are you sure you're not writing more characters than bytes of memory you allocated? Are you sure you terminated your c-string with '\0' character?

Did you already try using debugger to step trough the suspicious code and see what happens?



We are not using any kind of debugger in our project since we work directly on live servers.
So we have only some debug log files so we can printout some thing and see thats all we
can do :nottalkingtoyou:
Was This Post Helpful? 0
  • +
  • -

#10 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 09:51 AM

file1.c? Is it a typo? Are you compiling it all using C++ compiler, or do you compile C files with C compiler, C++ files with C++ compiler?

Your main problem is that you never allocate memory for the pointer you pass into fun(). You either do it before, and pass it's length too, or leave it to the function, and do it there.

edit:
void fun1(char* receivedString)
{
   string str;
   fun2(str);
   char* temp = str.c_str();
   receivedString = (char *) malloc(sizeof(char) * str.size()+1);
   for(int i=0; i<str.size; i++,receivedString++ temp++)
      *receivedString = temp;
   *receivedString = '\0';
}
See your loop - what is str.size? Forgot some parenthesis? ;)
receivedString++ temp++
Did you miss a comma?
*receivedString = temp;
Did you miss unary * ?

Also, sizeof(char) always results in 1 - on any architecture, any operating system. It's guaranteed by the standard. So you can as well just leave that one out. ;)

Also - what do you mean you don't use any debugger? O_o' Don't you have gdb installed? Even if you work through text terminal only, it's still not that hard to use it.
http://www.cs.cmu.ed...ilpin/tutorial/
http://www.unknownro...p.html#STOPEXEC

Using an IDE with debugging integration is many times easier. See NetBeans, Code::Blocks, probably even old and buggy Dev-C++ has that feature, and I believe many more also do.
If you happen to use Visual Studio, it has very nice debugging capabilities too.

This post has been edited by Xupicor: 11 June 2011 - 10:05 AM

Was This Post Helpful? 0
  • +
  • -

#11 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 10:09 AM

View PostXupicor, on 11 June 2011 - 09:51 AM, said:

file1.c? Is it a typo? Are you compiling it all using C++ compiler, or do you compile C files with C compiler, C++ files with C++ compiler?

Your main problem is that you never allocate memory for the pointer you pass into fun(). You either do it before, and pass it's length too, or leave it to the function, and do it there.

edit:
void fun1(char* receivedString)
{
   string str;
   fun2(str);
   char* temp = str.c_str();
   receivedString = (char *) malloc(sizeof(char) * str.size()+1);
   for(int i=0; i<str.size; i++,receivedString++ temp++)
      *receivedString = temp;
   *receivedString = '\0';
}
See your loop - what is str.size? Forgot some parenthesis? ;)
receivedString++ temp++
Did you miss a comma?
*receivedString = temp;
Did you miss unary * ?

Also, sizeof(char) always results in 1 - on any architecture, any operating system. It's guaranteed by the standard. So you can as well just leave that one out. ;)


I think its just typing mistake for size(), and comma and all

You mean to say I need to allocate memory in main() (file.c) and then it will work file!?
Was This Post Helpful? 0
  • +
  • -

#12 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 10:14 AM

Well I don't care if they're typing mistakes or a cat was sleeping on your keyboard, but since they're there - the compiler will care.

When I wrote that you never allocate memory I was referring to this post.
You then (before I submitted my answer) posted another source, which I commented in the edit.

This post has been edited by Xupicor: 11 June 2011 - 10:19 AM

Was This Post Helpful? 0
  • +
  • -

#13 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 08:58 PM

View PostXupicor, on 11 June 2011 - 10:14 AM, said:

Well I don't care if they're typing mistakes or a cat was sleeping on your keyboard, but since they're there - the compiler will care.

When I wrote that you never allocate memory I was referring to this post.
You then (before I submitted my answer) posted another source, which I commented in the edit.



As I said, I am not posting the original project code. Just typing sample code
which does same as my project code so there are some syntax errors so ignore that.

So, help me out with the problem please.
I am allocating memory after getting string from cpp file (so i wil be knowing the size)
so how to correct it.
Was This Post Helpful? 0
  • +
  • -

#14 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1620
  • View blog
  • Posts: 3,078
  • Joined: 30-May 10

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 09:54 PM

Is there some reason for the crazy mix of C and C++ here?

Anyway, since you failed to grasp my last comment, I'll spoon-feed it to you
void func ( char **stuffTheAnswerHere ) {
  char message[] = "stuff it";
  *stuffTheAnswerHere = malloc( strlen(message) + 1 );
  strcpy( *stuffTheAnswerHere, message );
}

int main ( ) {
  char *grabTheAnswer;
  func( &grabTheAnswer );
  printf("Hey, it's %s\n", grabTheAnswer );
  free( grabTheAnswer );
  return 0;
}



Now make sure you compile this as 'C', and make sure you include the right header files.
Was This Post Helpful? 2
  • +
  • -

#15 sunilchintu2468  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 37
  • Joined: 29-May 11

Re: using char* as reference variable and read from a function

Posted 11 June 2011 - 11:54 PM

View PostSalem_c, on 11 June 2011 - 09:54 PM, said:

Is there some reason for the crazy mix of C and C++ here?

Anyway, since you failed to grasp my last comment, I'll spoon-feed it to you
void func ( char **stuffTheAnswerHere ) {
  char message[] = "stuff it";
  *stuffTheAnswerHere = malloc( strlen(message) + 1 );
  strcpy( *stuffTheAnswerHere, message );
}

int main ( ) {
  char *grabTheAnswer;
  func( &grabTheAnswer );
  printf("Hey, it's %s\n", grabTheAnswer );
  free( grabTheAnswer );
  return 0;
}



Now make sure you compile this as 'C', and make sure you include the right header files.

Wow it's working fine... Thank you ..
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1