![]() |
| | #1 | |
| Registered User Join Date: Oct 2009
Posts: 3
| problem with rand() in pointers I am trying to enter a rand() in my code below, but it is causing a problem. It says 'a' not intialized. Since 'a' is a pointer I didn't think I had to have it equal to anything. I am a beginner at this so maybe I am missing something along the code...... Code:
// Program to process integers inputed by user in acending order and showing the number of times each number appears
// The use of rand() to investigate the random output and calloc to assign allocate memory for these integer inputed by user.
#include <stdio.h>
#include <stdlib.h>
int main (void)
{
int *a;
int i = 0, j=0, n, freq = 0;// input and increment variables
printf("Enter numbers from 1 to 10:");// input from user
scanf_s("%d", &n);
for(i = 0; i <= 7; i++)
{
*(a+i) = rand()%10 + 1;//creates random array(instead of scanf which allocates a memory to an array elements)
}
for (j =1;j <= 10; j++)// prints values inside of the array from 1 - 10
a = calloc(n, sizeof(int));// allocates memory for pointers, as random numbers can be entered by users
{
for ( i = 0; i <= 7; i++)//fills array - loops around until gone through all 8 elements
{
if (j == *(a+i)) //searches array for no. 1 to 10 and assigns value (j) to a[i]
{
++freq;//count of no of time a particular no. comes up
}
}
if(freq!=0)
printf("%d occurs %d times\n", j, freq);// output
freq = 0;
}
return 0;
}
This is the actual assignment: (Im not after the answer, just thought it might be useful plus im beyond frustrated...) Quote:
| |
| jugs is offline | |
| | #2 |
| Registered User Join Date: Jun 2008
Posts: 1,134
| Code: for(i = 0; i <= 7; i++)
{
*(a+i) = rand()%10 + 1;//creates random array(instead of scanf which allocates a memory to an array elements)
}
for (j =1;j <= 10; j++)// prints values inside of the array from 1 - 10
a = calloc(n, sizeof(int));// allocates memory for pointers, as random numbers can be entered by users
{
But from the comments I think you have the concept wrong. Pointers are variables that store memory addresses of other variables. When you do Code: calloc(n, sizeof(int)); Code: a = calloc(n, sizeof(int)); Now, when you dereference a pointer (with *) you read the value of the memory which its address is stored in the pointer. So if you do Code: *a = 0; So, when you dereference an unitialized pointer then you will try to write/read on a random memory location (somewhere in Texas probably). Which isn't good. The OS will not allow you to do so (since that memory can be reserved by another program for example). Dunno if you get the concept now. In any case, allocate first memory, assign the memory to a pointer and then write on where the memory points. |
| C_ntua is offline | |
| | #3 | ||||
| Algorithm Dissector Join Date: Dec 2005 Location: New Zealand
Posts: 2,475
| Quote:
Most of your comments are very wrong: Quote:
Quote:
Quote:
Where are all these wrong ideas of how things work coming from? You really need to read through what the things you use actually do. If you don't actually know for certain how some of the stuff you're using works, then you simply cannot program using them. In computer programming you absolutely need to understand what you're doing. "Do, or do not; there is no try".
__________________ My homepage Advice: Take only as directed - If symptoms persist, please see your debugger Last edited by iMalc; 11-11-2009 at 01:36 AM. | ||||
| iMalc is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Problem with file handling (pointers) | hmk | C Programming | 5 | 09-19-2008 10:03 AM |
| Problem with pointers | kotoko | C Programming | 3 | 06-12-2008 05:17 AM |
| A problem with pointers | vsla | C Programming | 2 | 10-10-2007 04:14 AM |
| Returning pointer to array of pointers problem | jimzy | C Programming | 15 | 11-11-2006 06:38 AM |
| Problem writing swap using pointers | joshdick | C++ Programming | 1 | 02-29-2004 10:06 PM |