473,890 Members | 5,884 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Making an array using Pointer without new and delete

3 New Member
Hi, i am trying to initialize a dynamic array and use this array to conduct linear search. I know about new and delete but as a pointer can be incremented by the sizeof its dataype, i wanted to create an array by incrementing a pointer!

This code's working fine just that when i enter the value to be stored in the array for linear search it says " POINTER (2) 0x2487 PROCESSOR FAULT"

Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int *a, *b, count, i,x,found,val,n;
  5.     char newN,ch;
  6.     b=a;
  7.  
  8.     printf("Enter the size of array : ");
  9.     scanf("%d", &n);
  10.  
  11.         count=0;
  12.         while(n!=0)
  13.         {    a++;
  14.             count++;
  15.             n--;
  16.         }
  17.         a=b;
  18.  
  19.     for(i=0;i<count;i++)
  20.     {
  21.         printf("Enter value for %d element ", i);
  22.         scanf("%d", & val);
  23.         a[i]=val;           /*Here's the problem lies, i've tried using & and *   operators but no help */
  24.     }
  25.  
  26.  
  27.     do{
  28.     found=0;
  29.     printf("\nEnter the element to be searched :");
  30.     scanf("%d", &x);
  31.  
  32.     for(i=0;i<count;i++)
  33.     {
  34.         if(a[i]==x)
  35.           {found=1;
  36.             break;
  37.           }
  38.     }
  39.     if(found==1)
  40.     printf("\nThe element has been found at position %d!",i+1);
  41.     else
  42.     printf("\n %d was not found!",x);
  43.  
  44.     fflush(stdin);
  45.     printf("\nSearch another element ? (Y/N) : ");
  46.     scanf("%c", &ch);
  47.     found=0;
  48.     }while(ch=='y'||ch=='Y');
  49. }
  50.  
Could anyone please sort it out for me?
Nov 4 '07 #1
3 1874
Ganon11
3,652 Recognized Expert Specialist
You never set a and b to point to anything. You declared them, and then set b equal to a (so they both point nowhere) and try to use them.

You're going to have to malloc some memory space, and let a and b [point to that. But, you run into a logic problem here. When you call malloc to allocate room for 1 int, the function looks for a space in memory big enough for 1 int, allocates it, and returns the address via a void*. But you are assuming that memory location has enough empty room in front of it for an unknown amount of ints. You might get lucky once in a while, and this wouldn't prove to be problematic. But more likely than not, you are going to push your pointer into other, already allocated memory, and mess up some possibly vital information.

I don't see any way you can accomplish what you want to do without malloc-ing a space big enough for n integers.

As a side note, your main should be declared

Expand|Select|Wrap|Line Numbers
  1. int main()
and return a 0 at the end of your program. void main() is non-standard and theoretically could do anything.
Nov 4 '07 #2
scruggsy
147 New Member
Hi, i am trying to initialize a dynamic array and use this array to conduct linear search. I know about new and delete but as a pointer can be incremented by the sizeof its dataype, i wanted to create an array by incrementing a pointer!
How and why?
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     int *a, *b, count, i,x,found,val,n;
  5.     char newN,ch;
  6.     b=a;
Assigning b to a is meaningless when a hasn't yet been initialized to any value.
Expand|Select|Wrap|Line Numbers
  1.     printf("Enter the size of array : ");
  2.     scanf("%d", &n);
  3.  
  4.         count=0;
  5.         while(n!=0)
  6.         {    a++;
  7.             count++;
  8.             n--;
  9.         }
  10.         a=b;
Here, you are trying to determine the size of a block of memory to store your "array", right? This doesn't work. You need to ask for memory to be allocated. You do that like this:
Expand|Select|Wrap|Line Numbers
  1. int a[20];
Or like this:
Expand|Select|Wrap|Line Numbers
  1. int* a = new int[n];
Nov 4 '07 #3
oler1s
671 Recognized Expert Contributor
i don't think our OP understands. Asking for dynamic memory is a runtime operation. Effectively, the operating system is asked to give up some memory if necessary. Actually, malloc/new have a bit of memory management. So either you use malloc or new, or you write your own whatever to allocate and manage memory.

It is possible to do this, and is done especially in cases where programmers need some really efficient memory allocation that new and malloc can't just do. But I don't think our OP gets this at all.
Nov 4 '07 #4

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

Similar topics

18
2276
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
23
7446
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these in an array. The application compiles but aborts without giving me any useful information. What I suspect is happening is infinite recursion. Each Directory object creates an array of Subdirectories each of which has an array of...
12
2587
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs about it. I wanted that on my class delete should not work. Object pointer should be deleted using my function only which is taking care of reference count for particular class. Thanx in advance for your inputs.
50
4536
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): //------------------------------------------------------------------ template<typename Data_t> class SmartPointer { Data_t* data; void(*deleterFunc)(Data_t*);
0
9979
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
9823
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,...
1
10924
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
10463
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 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...
0
9637
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
8017
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
5854
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...
0
6049
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4681
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 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.