473,324 Members | 2,501 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,324 software developers and data experts.

functions and arrays

63
void charfunc(char* );

char* charfuncptr(char* );

main()
{
char array[3]="ebc";

char* narr;

narr = array;

charfunc(array);

narr = charfuncptr(&array[0]);

printf("\nnew array is %s",narr);

printf("\nnew array is %s",array);

}


void charfunc(char* ptr) //send an string
{

printf("\nstring is %s", ptr);

}


char* charfuncptr(char* ptr)
{
//ptr[0]='s';
//ptr[1]='h';
//ptr[2]='a';
ptr="sha";
printf("\nmdified string is %s",ptr);
return ptr;

}

when I modify the pointer (ptr) in function (charfuncptr) as ptr="sha"
the array pointed by the pointer is not modified.
can anyone help on this
Feb 26 '08 #1
4 1410
Studlyami
464 Expert 256MB
First off with 62 posts you haven't learned how to use the CODE tags?
Second what compiler are you using?

char array[3]="ebc"; should throw an error. if you want to initialize the array = {'e','b','c'}; Second you are using "array" as a variable but it can be used as a keyword in c++ so i would look at using a different variable name.

When you pass an array into a function you are only passing a pointer not the array itself. So if you pass (array) you just pass a pointer to the first element of the array.

in your second function call you do (&array[0]) what are you hoping is the difference between the two function calls?

If you remove the (&array[0]) and replace it with just (array) when you do the ptr = "sha" the ptr address will change then when you return the ptr the array variable will have the old string and the narr variable will have the one you initalize in the charfuncptr. I would recommend reading up on arrays and how they are passed into functions. Also i would play around with it in a simple test program (kind of like what you have constructed above).
Feb 26 '08 #2
askcq
63
Since writing the above code would be very time consuming, C permits two alternate ways of achieving the same thing. First, one might write:

char my_string[40] = {'T', 'e', 'd', '\0',};

But this also takes more typing than is convenient. So, C permits:

char my_string[40] = "Ted";

array can be intialied by anyways above.....
Feb 26 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
when I modify the pointer (ptr) in function (charfuncptr) as ptr="sha"
the array pointed by the pointer is not modified.
can anyone help on this
It's because your function has a char* argument. Remember, this a copy of the pointer used to call the function. When the function changes the pointer, all it does is change the copy.

If you need to chnage the pointer used to call the function, then you bneed to pass in the address of that pointer, that is, a char**.
Feb 26 '08 #4
The code seems to have a few bizarre bits which could possibly be causing your problem. The first one and the one which I believe is most likely to be causing your problem is:
Expand|Select|Wrap|Line Numbers
  1. narr = charfuncptr(&array[0]);
  2.  
The '&' and '[0]' are unnecessary and could easily be causing a problem. With how your code is written at this point, both of the arrays 'char*' should equal the new array. I don't know if that is your intent but I thought I should mention it.
Feb 26 '08 #5

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

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
0
by: Bill | last post by:
I have some classes and functions in unmanaged C++ that I want to create a gui for using Windows Forms. The inputs to these functions (matrix multiplications, inversion,etc) take pointers to 2D...
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...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
5
by: Bern McCarty | last post by:
I have a DLL written in C++ (it's really C code that was adjusted to compile OK as C++) that I compile successfully into IL with the /CLR switch of Visual C 7.1. I use the resultant library...
4
by: Trent | last post by:
Still have problems with this thing. Seems my results are not matching the "correct" example given. The three sets of numbers below the last 3 columns is suppose to be the number of comparisons...
20
by: J de Boyne Pollard | last post by:
MThe library functions which are included to allow process Mlaunch, forking, and termination, imply that it is both Mpossible and desirable for a process to fork itself. This is Ma fundamental...
0
NeoPa
by: NeoPa | last post by:
Many of us have noticed that there are some very useful functions available to you when using Excel, but these same functions are not available, as standard, in Access. A particular issue I had...
3
by: Janwillem | last post by:
I want to make numerical functions that can be called from python. I am programming in pascal the last few decades so I had a look at "python for delphi" (P4D). The demo09 gives as example ...
127
by: sanjay.vasudevan | last post by:
Why are the following declarations invalid in C? int f(); int f(); It would be great if anyone could also explain the design decision for such a language restricton. Regards, Sanjay
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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...

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.