Hi,
I'm very new to C and need help with what I'm doing wrong here and why this piece of code doesn't work (the total column prints rubbish):
/* Total 2 arrays into a 3rd array in a function and return the pointer for the first element of the 3rd array. Display the 1st, 2nd and 3rd array in columns */ -
#include <stdio.h>
-
#define MAX34 5
-
-
long array3[MAX34] = {2,3,2,3,2};
-
long array4[MAX34] = {3,4,3,4,3};
-
long *point_a, *point_b;
-
long ctr = 0;
-
-
long * addarrays(long d[], long e[], long len);
-
-
main()
-
{
-
-
point_a = addarrays(array3, array4, MAX34);
-
-
point_b = point_a;
-
-
for (ctr = 0; ctr < MAX34; ctr++)
-
{
-
printf("\n\t%ld\t\%ld\t%ld\n", array3[ctr], array4[ctr], *(point_b++));
-
}
-
-
-
return 0;
-
}
-
-
-
long * addarrays(long d[], long e[], long len)
-
{
-
long ctr = 0;
-
long array5[] = {0,0,0,0,0};
-
long *newpoint;
-
-
newpoint = array5;
-
-
for (ctr = 0; ctr < len; ctr++)
-
{
-
array5[ctr] = array3[ctr] + array4[ctr];
-
}
-
-
return newpoint;
-
}
-
5 1192 Banfa 9,065
Recognized Expert Moderator Expert -
long * addarrays(long d[], long e[], long len)
-
{
-
long ctr = 0;
-
long array5[] = {0,0,0,0,0};
-
long *newpoint;
-
-
/* newpoint points to array5, array5 is on the stack */
-
newpoint = array5;
-
-
for (ctr = 0; ctr < len; ctr++)
-
{
-
/* You are using your global variables in stead of the function parameters */
-
array5[ctr] = array3[ctr] + array4[ctr];
-
}
-
-
/* You return newpoint, but newpoint points to array5 and array5 is on the stack. As soon as the function returns array5 is released so you are returning a pointer to data that has just been relesed and is almost certainly used for something else by printf. When returning a pointer it is imperative to make sure that the data you are returning a pointer to still exists after the function has ended. In this particular case I would say the best thing would be to pass as a 4th parameter a pointer to the location to store the result of the function, however another option is to malloc data for the function to return but the the calling function has to remember to free it. */
-
return newpoint;
-
}
-
Thanks for the reply, but I'm a bit lost. This was an exercise from "Teach yourself C in 21 days" - day 9, before malloc has been discussed. Unfortunately, it's an ON YOUR OWN exercise, so no answer is given at the back of the book, and the chapter doesn't have any examples of how to do this. I don't really understand your first option - would it be rude to ask for an example?
Banfa 9,065
Recognized Expert Moderator Expert
Instead of -
#include <stdio.h>
-
#include <string.h>
-
-
static char *ReturnHelloWorld(void);
-
-
int main()
-
{
-
printf("%s\n", ReturnHelloWorld());
-
-
return 0;
-
}
-
-
char *ReturnHelloWorld(void)
-
{
-
char string[50];
-
-
strcpy(string, "Hello World");
-
-
return string;
-
}
-
which would have the same problem as your code, returning a pointer to an object that no longer exists do this -
#include <stdio.h>
-
#include <string.h>
-
-
static void ReturnHelloWorld(char *buffer);
-
-
int main()
-
{
-
char string[50];
-
-
ReturnHelloWorld(string)
-
-
printf("%s\n", string);
-
-
return 0;
-
}
-
-
void ReturnHelloWorld(char *buffer)
-
{
-
strcpy(buffer, "Hello World");
-
}
-
Now the result buffer is passed to the function by the caller. The caller has the responsibility for the data. This would be even better if ReturnHelloWorld also took a buffer length and ensured that the end of the buffer was not overwritten thus ensuring the function could not cause memory corruption.
Try both examples (there may be some compilation errors I have not been able to do a test compile as I have just switched from Windows to Linux and I have yet to get a development environment setup again).
Thanks for your help - got it working at last.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
by: James Vanns |
last post by:
Sounds nasty doesn't it!! Well it's kinda what I need to do! I have an
external C struct (external to the C++ project/classes etc.) which is
wants a function ptr assigned to one of it's members:
...
|
by: Ian Robertson |
last post by:
I am trying to write a function that takes a reference to an object as its
arguement. The object is created in another function and I am trying to pass
the object to another function from within...
|
by: Thomas Christmann |
last post by:
Hi!
Sorry for the weird topic, I don't know how to describe it better...
I have a little problem here I can't wrap my mind around. If I do:
-------------------------------------
#define DWORD...
|
by: truckaxle |
last post by:
I am trying to pass a slice from a larger 2-dimensional array to a
function that will work on a smaller region of the array space. The
code below is a distillation of what I am trying to...
| |
by: dogalacar |
last post by:
Hi All,
I am trying to pass array of structures from a C dll to C# as msdn
sample does(outarrayofstructs sample) but PtrToStructure function gives
error :
--> "structure must not be a value...
|
by: Mike |
last post by:
Consider the following code:
"""
struct person {
char *name;
int age;
};
typedef struct person* StructType;
|
by: TS |
last post by:
I was under the assumption that if you pass an object as a param to a method
and inside that method this object is changed, the object will stay changed
when returned from the method because the...
|
by: S. |
last post by:
Hi all,
Can someone please help me with this?
I have the following struct:
typedef struct {
char *name;
int age;
} Student;
|
by: CapCity |
last post by:
I'm sure I'm missing something simple - I do not code in C regularly, and
the breaks are long enough for me to forget.
The situation I have is I need to create an array but I do not know the...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |