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" -
#include<stdio.h>
-
void main()
-
{
-
int *a, *b, count, i,x,found,val,n;
-
char newN,ch;
-
b=a;
-
-
printf("Enter the size of array : ");
-
scanf("%d", &n);
-
-
count=0;
-
while(n!=0)
-
{ a++;
-
count++;
-
n--;
-
}
-
a=b;
-
-
for(i=0;i<count;i++)
-
{
-
printf("Enter value for %d element ", i);
-
scanf("%d", & val);
-
a[i]=val; /*Here's the problem lies, i've tried using & and * operators but no help */
-
}
-
-
-
do{
-
found=0;
-
printf("\nEnter the element to be searched :");
-
scanf("%d", &x);
-
-
for(i=0;i<count;i++)
-
{
-
if(a[i]==x)
-
{found=1;
-
break;
-
}
-
}
-
if(found==1)
-
printf("\nThe element has been found at position %d!",i+1);
-
else
-
printf("\n %d was not found!",x);
-
-
fflush(stdin);
-
printf("\nSearch another element ? (Y/N) : ");
-
scanf("%c", &ch);
-
found=0;
-
}while(ch=='y'||ch=='Y');
-
}
-
Could anyone please sort it out for me?
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
and return a 0 at the end of your program. void main() is non-standard and theoretically could do anything.
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? -
#include<stdio.h>
-
void main()
-
{
-
int *a, *b, count, i,x,found,val,n;
-
char newN,ch;
-
b=a;
Assigning b to a is meaningless when a hasn't yet been initialized to any value. -
printf("Enter the size of array : ");
-
scanf("%d", &n);
-
-
count=0;
-
while(n!=0)
-
{ a++;
-
count++;
-
n--;
-
}
-
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:
Or like this: 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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Vasileios Zografos |
last post by:
Hello,
can anyone please tell me if there is any difference between the two:
double Array1;
and
|
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...
|
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.
|
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*);
|
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...
| |
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: 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,...
|
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: 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...
|
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
| |