473,382 Members | 1,445 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,382 software developers and data experts.

Making an array using Pointer without new and delete

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 1852
Ganon11
3,652 Expert 2GB
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 100+
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 Expert 512MB
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
by: Vasileios Zografos | last post by:
Hello, can anyone please tell me if there is any difference between the two: double Array1; and
23
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...
12
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...
50
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): ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...

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.