473,320 Members | 2,012 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

passing a (char *) as an argument

19
Hi,

I try to write in C a function that can edit a string allocated dynamically. I tried:

Expand|Select|Wrap|Line Numbers
  1. int funct(char ** str)
  2. {
  3. int n;
  4. n=strlen("TEST");
  5. (*str)=malloc(sizeof(char)*(n+1));
  6. strcpy(*str,"TEST");
  7. }
the main is

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char * str;
  4. funct(&str);
  5. printf("%s\n",str);
  6. }
It's not the way to do it as it only "printf" garbages.

Can you help?

Regards
Sep 21 '07 #1
7 2833
Hi!

Your code seems correct to me.

Make sure you've included stdio.h stdlib.h and string.h.

I also noticed that your functions don't return anything but I don't think it's the cause of your problem.
Sep 21 '07 #2
Savage
1,764 Expert 1GB
Hi,

I try to write in C a function that can edit a string allocated dynamically. I tried:

Expand|Select|Wrap|Line Numbers
  1. int funct(char ** str)
  2. {
  3. int n;
  4. n=strlen("TEST");
  5. (*str)=malloc(sizeof(char)*(n+1));
  6. strcpy(*str,"TEST");
  7. }
the main is

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3. char * str;
  4. funct(&str);
  5. printf("%s\n",str);
  6. }
It's not the way to do it as it only "printf" garbages.

Can you help?

Regards
Your function doesn't need to have a pointer to a pointer to a char as argument,it can only have a pointer to the char.

Savage
Sep 21 '07 #3
Your function doesn't need to have a pointer to a pointer to a char as argument,it can only have a pointer to the char.
I do not agree with you. str is not malloc'd in main(), so its address has to be passed to the function in order to initialize it and to make it available in the main function. However, it is not needed to fill the string.

Expand|Select|Wrap|Line Numbers
  1. int funct_alloc(char ** pstr)
  2. {
  3.   int n;
  4.  
  5.   n=strlen("TEST");
  6.   (*pstr)=malloc(sizeof(char)*(n+1));
  7. }
  8.  
  9. void funct_fill(char * str)
  10. {
  11.   strcpy(str,"TEST");
  12. }
  13.  
  14. int main()
  15. {
  16.   char * str;
  17.  
  18.   funct_alloc(&str);
  19.   funct_fill(str);
  20.   printf("%s\n",str);
  21.  
  22.   return 0;
  23. }
Sep 21 '07 #4
Savage
1,764 Expert 1GB
I do not agree with you. str is not malloc'd in main(), so its address has to be passed to the function in order to initialize it and to make it available in the main function. However, it is not needed to fill the string.

Expand|Select|Wrap|Line Numbers
  1. int funct_alloc(char ** pstr)
  2. {
  3.   int n;
  4.  
  5.   n=strlen("TEST");
  6.   (*pstr)=malloc(sizeof(char)*(n+1));
  7. }
  8.  
  9. void funct_fill(char * str)
  10. {
  11.   strcpy(str,"TEST");
  12. }
  13.  
  14. int main()
  15. {
  16.   char * str;
  17.  
  18.   funct_alloc(&str);
  19.   funct_fill(str);
  20.   printf("%s\n",str);
  21.  
  22.   return 0;
  23. }
When you have a function that takes a pointer as a argument,that's pass by reference.If you don't believe try this sample code below,and see it for yourself

Expand|Select|Wrap|Line Numbers
  1. void swap(int *a,int n)
  2. {
  3.      int temp;
  4.      temp=a[0];
  5.      a[0]=a[n];
  6.      a[n]=temp;
  7. }
  8.  
  9. int main(int argc, char *argv[])
  10. {
  11.     int a[3],i;
  12.     for(i=0;i<3;i++) a[i]=i;
  13.     swap(a,2);
  14.  
  15.     for(i=0;i<3;i++) cout<< a[i]<<" "; 
  16.  
  17.     return 0;
  18. }
Savage
Sep 21 '07 #5
JosAH
11,448 Expert 8TB
If you don't believe try this sample code below
I don't believe you either; the OP and ruskalym are right. Don't talk about 'pass
by reference' when you're talking C; it doesn't exist in that language.

kind regards,

Jos
Sep 21 '07 #6
Savage
1,764 Expert 1GB
I don't believe you either; the OP and ruskalym are right. Don't talk about 'pass
by reference' when you're talking C; it doesn't exist in that language.

kind regards,

Jos
I know that it doesn't exist, but it can be simulated with a pointer.

Savage
Sep 21 '07 #7
Savage
1,764 Expert 1GB
Everyone,sorry about my nonsenses I'm a bit dis concentrated last few days.

Savage
Sep 21 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
17
by: BlindHorse | last post by:
Help!!! I need someone to tell me why I am getting the err msg error C2440: '=' : cannot convert from 'char *' to 'char' //==================== #include <iostream>
1
by: Foxy Kav | last post by:
Hi everyone, im a first year UNI student doing a programming subject and im stuck on how to get rid of my global variables, char stringarray and char emptystring. I was wondering if anyone could...
3
by: Goh, Yong Kwang | last post by:
I'm trying to create a function that given a string, tokenize it and put into a dynamically-sized array of char* which is in turn also dynamically allocated based on the string token length. I...
10
by: Pete | last post by:
Can someone please help, I'm trying to pass an array to a function, do some operation on that array, then return it for further use. The errors I am getting for the following code are, differences...
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
9
by: Juggernaut | last post by:
I am trying to create a p_thread pthread_create(&threads, &attr, Teste, (void *)var); where var is a char variable. But this doesnt't work, I get this message: test.c:58: warning: cast to pointer...
3
by: Amit_Basnak | last post by:
Dear friends I have to pass the objec of a class which is a part of afunction in thefunction call. my code looks like this now #include <iostream.h> #include <waspc/common.h> #include...
6
by: Andy Baker | last post by:
I am attempting to write a .NET wrapper for a C++ DLL file, but am having problems with passing strings as parameters. How should I be writing my C# function call when the C header file is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.