473,399 Members | 3,919 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,399 software developers and data experts.

Regarding array operations using pointers.....

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 1866
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
Thanks.Worked fine.I guess I'll have to revise break keyword again.
Had to manipulate some &&,|| operators.
Jun 1 '10 #3
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
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
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
@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
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
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...
19
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
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...
3
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? ...
5
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
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"...
6
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
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
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...
0
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...

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.