Hi, can some one please tell me why this program is not able to
function properly. I have a array a and i am trying to create a
pointer array b which points to elements less than 40 in a.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void create_ptr_list (int *a, int ***b, int n, int *size_ptr)
{
int i;
*size_ptr = 0;
for (i = 0; i < n; i++)
{
if (a[i] < 40)
(*size_ptr) ++;
}
*b = malloc(sizeof(i nt *) * (*size_ptr));
(*size_ptr) = 0;
for (i = 0; i < n; i++)
{
if (a[i] < 40)
(*b)[*size_ptr++]= &a[i];
}
}
int main(void)
{
int a[] = { 5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
int **b;
int size;
int i;
create_ptr_list (a, &b, 10, &size);
for(i = 0; i <size; i++)
printf("%d\n", *(b[i]));
return (0);
}
Jun 30 '08
13 1885
pereges wrote:
On Jun 30, 9:33 pm, pete <pfil...@mindsp ring.comwrote:
>I would write that this way:
/* BEGIN new.c */
#include <stdio.h> #include <stdlib.h>
int *create_ptr_lis t(int *a, size_t n, size_t *size_ptr) { int *b; size_t i;
*size_ptr = 0; for (i = 0; n i; ++i) { if (40 a[i]) { ++*size_ptr; } } b = malloc(*size_pt r * sizeof *b); *size_ptr = 0; if (b != NULL) { for (i = 0; n i; i++) { if (40 a[i]) { b[(*size_ptr)++]= a[i]; } } } return b;
}
int main(void) { int a[] = {5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1}; int *b; size_t size; size_t i;
b = create_ptr_list (a, 10, &size); if (b != NULL) { for (i = 0; size i; ++i) { printf("%d\n", b[i]); } } else { puts("b == NULL"); } free(b); return 0;
}
/* END new.c */
I actually need an array of pointers.
Then, I would write that this way:
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
int **create_ptr_li st(int *a, size_t n, size_t *size_ptr)
{
int **b;
size_t i;
*size_ptr = 0;
for (i = 0; n i; ++i) {
if (40 a[i]) {
++*size_ptr;
}
}
b = malloc(*size_pt r * sizeof *b);
*size_ptr = 0;
if (b != NULL) {
for (i = 0; n i; i++) {
if (40 a[i]) {
b[(*size_ptr)++]= a + i;
}
}
}
return b;
}
int main(void)
{
int a[] = {5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
int **b;
size_t size;
size_t i;
b = create_ptr_list (a, 10, &size);
if (b != NULL) {
for (i = 0; size i; ++i) {
printf("%d\n", *b[i]);
}
} else {
puts("b == NULL");
}
free(b);
return 0;
}
/* END new.c */
--
pete
[warning: all my code here is untested]
In article <OM************ *************** ***@earthlink.c om>
pete <pf*****@mindsp ring.comwrote:
>#include <stdio.h> #include <stdlib.h>
int **create_ptr_li st(int *a, size_t n, size_t *size_ptr) {
int **b;
size_t i;
*size_ptr = 0;
for (i = 0; n i; ++i) {
if (40 a[i]) {
++*size_ptr;
}
}
b = malloc(*size_pt r * sizeof *b);
*size_ptr = 0;
if (b != NULL) {
for (i = 0; n i; i++) {
if (40 a[i]) {
b[(*size_ptr)++]= a + i;
}
}
}
return b; }
I never really understand what people have against local variables :-)
I would begin by rewriting the above function as:
/*
* Create an array of pointers to elements that exceed the
* specified limit. The input array is a[i], where 0 <= i < n,
* and the limit is the given value. The size of the pointer
* array is placed in *size_ptr.
*/
int **create_ptr_li st(int *a, size_t n, int limit, size_t *size_ptr) {
int **b;
size_t i, over_limit;
for (i = over_limit = 0; i < n; i++)
if (a[i] limit)
over_limit++;
b = malloc(over_lim it * sizeof *b);
if (b != NULL) {
for (i = over_limit = 0; i < n; i++)
if (a[i] limit)
b[over_limit++] = &a[i];
*size_ptr = over_limit;
} else {
/* optionally: handle error here if over_limit 0 */
*size_ptr = 0;
}
return b;
}
More generally, one might pass the function a pointer to another
function that selects elements that pass some criterion or
criteria. This does make things substantially more complicated,
and possibly significantly slower (depending on how long it takes
to call functions):
int **create_ptr_li st(int *a,
size_t n,
int (*match)(int, void *),
void *context,
size_t *size_ptr) {
int **b;
size_t i, nmatch;
for (i = nmatch = 0; i < n; i++)
if (match(a[i], context))
nmatch++;
b = malloc(nmatch * sizeof *b);
... the rest is obvious ...
}
We can get also rid of the "int" restriction by changing "int *a"
to "void *base" and adding a "size" parameter. Now, however, we
must build an array of "void *"s, so this may be going too far
(and indeed the match-with-context may already be too far). This
time I will verify that the match function does not change its
mind though:
void **create_ptr_li st(void *base0, size_t n, size_t size,
int (*match)(void *, void *),
void *context,
size_t *size_ptr) {
unsigned char *base = base0;
void **b;
size_t i, nmatch1, nmatch2;
for (base = base0, i = nmatch1 = 0; i < n; base += size, i++)
if (match(base, context))
nmatch1++;
b = malloc(nmatch1 * sizeof *b);
if (b != NULL) {
for (base = base0, i = nmatch2 = 0; i < n; base += size, i++) {
if (match(base, context) {
if (nmatch2 >= nmatch1)
... handle error ...
b[nmatch2++] = base;
}
}
... optionally, make sure nmatch2 == nmatch1 ...
*size_ptr = nmatch2;
} else {
/* optionally: handle error here if over_limit 0 */
*size_ptr = 0;
}
return b;
}
>int main(void) {
int a[] = {5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
int **b;
size_t size;
size_t i;
b = create_ptr_list (a, 10, &size);
Seems like this should be:
b = create_ptr_list (a, sizeof a / sizeof a[0], &size);
if (b != NULL) {
for (i = 0; size i; ++i) {
printf("%d\n", *b[i]);
}
} else {
puts("b == NULL");
}
free(b);
return 0; }
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: gmail (figure it out) http://web.torek.net/torek/index.html
On 30 Jun, 16:33, pereges <Brol...@gmail. comwrote:
<snip>
int main(void)
{
* *int a[] = { 5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1};
<snip>
there are 11 elements in this array
* * create_ptr_list (a, &b, 10, &size);
you pass 10 to this function. Is this correct?
I use a macro like this
#define ARRAY_SIZE(A) (sizeof(A)/sizeof(*A))
(oops! another use for #define!)
<snip>
--
Nick Keighley
"If builders built houses the way programmers write programs,
the first woodpecker coming along would destroy civilization"
Chris Torek wrote:
[warning: all my code here is untested]
In article <OM************ *************** ***@earthlink.c om>
pete <pf*****@mindsp ring.comwrote:
>int main(void) { int a[] = {5, -6, 45, -100, 20, -150, 160, 40, 0, 0, 1}; int **b; size_t size; size_t i;
b = create_ptr_list (a, 10, &size);
Seems like this should be:
b = create_ptr_list (a, sizeof a / sizeof a[0], &size);
.... especially with eleven elements in the array.
--
pete This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Charles |
last post by:
I am going through the exercises and Bruce Eckel's Thinking in C++ and
I ran into an exercise that wasn't included in his solutions that I
think I could use some assistance with.
Exercise 3-26 pg 213:
Define an array of int. Take the starting address of that array and
use static_cast to convert it into an void*. Write a function that
takes a void*, a number (indicating a number of bytes), and a value
(indicating the value to which...
|
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 code...
TCHAR myArray;
DoStuff(myArray);
|
by: Davy |
last post by:
Hi all,
I found
char x={"my"};
can be compiled.
But
char x;
x={"my"};
can not be compiled.
|
by: Edd Dawson |
last post by:
Hi. I have a strange problem involving the passing of command line
arguments to a C program I'm writing. I tried posting this in
comp.programming yesterday but someone kindly suggested that I'd have
better luck here. So here goes!
My program ignores any command line arguments, or at least it's
supposed to. However, when I pass any command line arguments to the
program, the behaviour of one of the functions changes mysteriously. I
have...
|
by: intrepid_dw |
last post by:
Hello, all.
I've created a C# dll that contains, among other things, two functions
dealing with byte arrays. The first is a function that returns a byte
array, and the other is intended to receive a byte array as one of its
parameters. The project is marked for COM interop, and that all
proceeds normally.
When I reference the type library in the VB6 project, and write the
code to call the function that returns the byte array, it works
| |
by: Wild Wind |
last post by:
Hello,
I made a post relating to this issue a while
back, but I haven't received any answer, so
here I am again.
I am writing a mixed C++ dll which uses the following
declaration:
typedef System::Byte ByteArray __gc;
|
by: Martin Jørgensen |
last post by:
Hi,
I'm relatively new with C-programming and even though I've read about
pointers and arrays many times, it's a topic that is a little confusing
to me - at least at this moment:
----
1) What's the difference between these 3 statements:
(i) memcpy(&b, &KoefD, n); // this works somewhere in my code
|
by: Flash Gordon |
last post by:
Reading the standard, va_list is an object type (, so I believe the
following should be possible:
#include <stdarg.h>
void foo(va_list *arg)
{
/* do some stuff which conditionally might read parameters off arg */
}
|
by: venkatagmail |
last post by:
I have problem understanding pass by value and pass by reference and
want to how how they are or appear in the memory:
I had to get my basics right again. I create an array and try all
possible ways of passing an array. In the following code, fun1(int
a1) - same as fun1(int* a1) - where both are of the type passed by
reference. Inside this function, another pointer a1 is created whose
address &a1 is different from that of the passed...
|
by: jason |
last post by:
Hello,
I'm a beginning C programmer and I have a question regarding arrays and
finding the number of entries present within an array.
If I pass an array of structures to a function, then suddenly I can't use
sizeof(array) / sizeof(array) anymore within that function ?
Help - What point am I missing ?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |