473,799 Members | 2,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Regarding array operations using pointers.....

23 New Member
I am trying to print prime numbers from 1 to 100. I am passing an array to a function and then zeroing all the elements which are multiples of 2,3,5. When I compile the code,I am getting all the memory addresses as an output. Here is the code that I have written.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define size 100
  4. int main()
  5. {
  6.     int i,a[size];
  7.     int prime(int *,int);
  8.  
  9.     for(i=1;i<size+1;i++)
  10.     {
  11.         a[i]=i;
  12.     }
  13.     printf("Before procedure the array is\n");
  14.     for(i=1;i<size+1;i++)
  15.     {
  16.  
  17.         printf("%d\n",a[i]);
  18.     }
  19.     prime(&a[0],size);
  20.  return 0;
  21. }
  22. int prime(int *ptr,int j)
  23. {
  24.     int k;
  25.     ptr++;
  26.     for(k=1;k<j+1;k++)
  27.     {
  28.         if(*ptr==2||*ptr==3||*ptr==5)
  29.         {
  30.             break;
  31.         }
  32.         else
  33.         {
  34.             if(*ptr%2==0||*ptr%3==0||*ptr%5==0)
  35.             {
  36.                 *ptr=0;
  37.             }
  38.         }
  39.         ptr++;
  40.     }
  41.  
  42. ptr=ptr-99;
  43.     printf("Array after removal\n");
  44.  
  45.     for(k=1;k<j+1;k++)
  46.     {
  47.         if(*ptr==0)
  48.         {
  49.             break;
  50.         }
  51.         else
  52.         {
  53.             printf("%d\n",*ptr);
  54.  
  55.         }
  56.         ptr++;
  57.     }
  58. }
  59.  
Further can I anyone of the moderators enlighten me on pointer arithmetic in context of arrays and functions. I have read arrays revealed article but it didn't give me much information.
Jun 1 '10 #1
6 1896
mayankarpit
13 New Member
He is not printing adrress. infact he is printing garbage values. this is happening bcoz of break stament at line 30. As soon as *ptr == 2 u break from for loop and do ptr = ptr - 99. Bcoz of this ptr starts pointing some garbage location so when loop stars at line 45 it start with printing garbage values
Jun 1 '10 #2
abhinuke
23 New Member
Thanks.Worked fine.I guess I'll have to revise break keyword again.
Had to manipulate some &&,|| operators.
Jun 1 '10 #3
swatib2k101
4 New Member
I have one pointer to string ,
char *str[4];
i am calling str[1] in a func say make func()
How to do it.Seg fault is coming.
char *str[4];
makefunc(&str);

char makefunc(char *pr[])
How to do it?Please help
Basically I want to call this str[1] in other function and prints its value.
Jun 2 '10 #4
akshayshukla
1 New Member
You can call function like this.
makefunc(&pr[1]));

and define function like this.
char makefunc(char *pr)
{
printf("Passed value is %d", *ptr);
}
Jun 2 '10 #5
mayankarpit
13 New Member
Solution suggested by akshay will not solve the segv.Yo have declared a array of char. pointer & before paasing to function you never initialsed it.
Jun 2 '10 #6
akinidu
7 New Member
@akshayshukla
WHy dont u post the revised code tooo
Jun 2 '10 #7

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

Similar topics

10
6569
by: Noah Spitzer-Williams | last post by:
Hello guys, I'm itinerating through my array using pointers in this fashion: image is unsigned char image do { cout << "image byte is: " << *image << endl;
138
5297
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that array(i.e)it will have the address of the first element.In the the program below am not able to increment the value stored in x,which is the address of the first element.Why am I not able to do that?Afterall 1 is also a hexadecimal number then...
19
14525
by: gaga | last post by:
I can't seem to get this to work: #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *names; char **np;
12
4759
by: natkw1 | last post by:
Hi, I'm attempting to understand the use of pointers(at least grasp how pointers work). I've read the FAQ on http://www.eskimo.com/~scs/C-faq/s6.html on pointers and arrays but I'm still a bit lost. I written the following code to try to understand it but it's not working:
3
6702
by: Bilgehan.Balban | last post by:
Hi, How do I declare an array of function pointers and assign each element of the array with public member functions of a class? Is it possible that the array is not a member of the class? Thanks, Bahadir
5
1891
by: aki | last post by:
Hi all, can anyone tell me the syntax of declaring a 2D array of function pointers.... and initializing it.. thanks aki
3
1682
by: Scotty | last post by:
I'm a C++ novice and need help figuring out some odd behavior I've encountered. Here's the situation: I create a class and have its constructor store a random number in a private "number" variable. The class also has a "getNumber()" function that returns its stored number. A function in the program creates five objects of this class and five pointers to these objects, the latter of which are stored in a global array. When "getNumber()"...
6
3519
by: M Turo | last post by:
Hi, I was wondering if anyone can help. I'm want to pre-load a 'table' of function pointers that I can call using a its arrayed index, eg (non c code example) pFunc = func_A; pFunc = func_B;
4
10424
by: Voevoda | last post by:
hello, I'm in a hurry I got an exam monday and I haven't managed to copy a string into and array of characters to treat the letters individually, how do I do that ?thank you, please it's very urgent !! And I'm a beginner I don't know much about pointers, if anything.
0
9688
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
10491
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
10268
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
10247
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
9079
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...
1
7571
isladogs
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6809
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();...
1
4146
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
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.