473,790 Members | 3,200 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem passing pointer array


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 #1
13 1883
On Jun 30, 6:33 pm, pereges <Brol...@gmail. comwrote:
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)
Change int ***b to int **b.
Change the 'int **b' in your main() to int *b.
Jun 30 '08 #2
On Jun 30, 8:59 pm, vipps...@gmail. com wrote:
Change int ***b to int **b.
Change the 'int **b' in your main() to int *b.
Wouldn't int *b lead to an array instead of array of pointers ? I
needed array of pointers hence int **b.
Jun 30 '08 #3
pereges wrote:
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);
}
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 */

The distinction between counters and sortable data
is more obvious with size_t counters.
I don't like to read sorting functions
where everything is int.

--
pete
Jun 30 '08 #4
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. The array b will contains
pointers to elements in a which are less than 40.
The distinction between counters and sortable data
is more obvious with size_t counters.
I don't like to read sorting functions
where everything is int.
Sometimes using size_t or any unsigned entity can cause trouble in
sorting algorithms depending on how we write the sorting algorithm.
eg. just check the post i made on a quick sort algo today.
Jun 30 '08 #5
On Jun 30, 7:15 pm, pereges <Brol...@gmail. comwrote:
On Jun 30, 8:59 pm, vipps...@gmail. com wrote:
Change int ***b to int **b.
Change the 'int **b' in your main() to int *b.

Wouldn't int *b lead to an array instead of array of pointers ? I
needed array of pointers hence int **b.
Sorry, I just realized that.
The problem in your original code is in this line:
(*b)[*size_ptr++]= &a[i];
that increments 'size_ptr' as a pointer, not the value it points to.
Change it to (*b)[(*size_ptr)++] = &a[i];
Jun 30 '08 #6
On Jun 30, 9:51 pm, vipps...@gmail. com wrote:
Sorry, I just realized that.
The problem in your original code is in this line: (*b)[*size_ptr++]= &a[i];

that increments 'size_ptr' as a pointer, not the value it points to.
Change it to (*b)[(*size_ptr)++] = &a[i];
Thanks, that solved it. I realize it was a stupid mistake
Jun 30 '08 #7
On Jun 30, 7:51 pm, vipps...@gmail. com wrote:
On Jun 30, 7:15 pm, pereges <Brol...@gmail. comwrote:
On Jun 30, 8:59 pm, vipps...@gmail. com wrote:
Change int ***b to int **b.
Change the 'int **b' in your main() to int *b.
Wouldn't int *b lead to an array instead of array of pointers ? I
needed array of pointers hence int **b.

Sorry, I just realized that.
The problem in your original code is in this line: (*b)[*size_ptr++]= &a[i];

that increments 'size_ptr' as a pointer, not the value it points to.
Change it to (*b)[(*size_ptr)++] = &a[i];
You also don't check the return value of malloc. It could be NULL, in
which case you just return; the caller can check for the
successfulness of create_ptr_list (a, &b, c, &d); with if(b !=
NULL) ...

Also the design is flawed, mainly because this works only for arrays
of ints, and because 40 is hardcoded, how about you change this to
struct vector { size_t nmemb, size; void *elements; };
struct vector *remove_if(cons t struct vector *v, int (*remove)(void
*));

though I don't like the 'remove_if' name, I chose it because there's a
similar function in common lisp named REMOVE-IF.
Jun 30 '08 #8
On Jun 30, 10:00 pm, vipps...@gmail. com wrote:
You also don't check the return value of malloc. It could be NULL, in
which case you just return; the caller can check for the
successfulness of create_ptr_list (a, &b, c, &d); with if(b !=
NULL) ...

Also the design is flawed, mainly because this works only for arrays
of ints, and because 40 is hardcoded, how about you change this to
struct vector { size_t nmemb, size; void *elements; };
struct vector *remove_if(cons t struct vector *v, int (*remove)(void
*));

though I don't like the 'remove_if' name, I chose it because there's a
similar function in common lisp named REMOVE-IF.
Well, I wrote the function just as a test function to understand about
array of pointers. Just chose int to make things simple.
Jun 30 '08 #9
pereges wrote:
On Jun 30, 9:33 pm, pete <pfil...@mindsp ring.comwrote:
>I would write that this way:
I actually need an array of pointers.
Sorry about that.

--
pete
Jun 30 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2540
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...
58
10182
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);
28
2131
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
6
2937
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...
8
10719
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
3
1778
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;
39
19650
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
7
2863
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 */ }
11
3365
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...
20
2186
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 ?
0
9666
marktang
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9512
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,...
0
10419
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10201
jinu1996
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...
1
10147
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9023
agi2029
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...
0
5552
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4100
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
3
2910
bsmnconsultancy
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.