How to Assign Inputs as Addresses in C Programming?

In summary, the programmer has a problem trying to assign an input as an adress to a specified value.
  • #1
akueddy
14
0
Hi guys,

***I just started c programming, I've done some reading last week and i came up with a basic program.Ive got a problem trying to assign an input as an adress to a specified value.Basically i have 3 datas in my array and it will be called by inputting a number which points to the adress.

***Here's the program that i wrote :


#include <stdio.h>


void main()
{

int n;

int Array[3];
Array[0] = 10;
Array[1] = 20;
Array[2] = 30;

int *pArray;

pArray = &Array[n];

printf("choose a number 0-2\n");
scanf("%d", &n);

pArray = &Array[n];

printf("because you choose that number ill give you : $%d\n",*pArray);
}

-------------------------------------------------------------------------------------

***Although there's an error message, the program runs just fine...


C:\Borland\BCC55\Bin>bcc32 pointers4
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
pointers4.cpp:
Warning W8004 pointers4.cpp 16: 'pArray' is assigned a value that is never used
in function main()
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland

C:\Borland\BCC55\Bin>pointers4
choose a number 0-2
2
because you choose that number ill give you : $30

C:\Borland\BCC55\Bin>

--------------------------------------------------------------------------------------


***here's my question :

1) Can someone explain to me what's the warning all about and how to correct it? i though i have assign a value for 'pArray' according to the user's input.
2) What if i want to have a really large amount of datas stored in my array, let's say 100 datas. Is there any simple way of doing it say :

Array[10,20,30,40,50,60,70...1000]?
 
Technology news on Phys.org
  • #2
  1. You're assigning to pArray and then assigning to it again without using the value you first assigned to it. Furthermore, you're using n before it's been initialised, which you should never do.
  2. There are two ways of doing this. You could either assign them each individually:
    Code:
    int array[] = { 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 };
    …or if there are loads, you could use a for loop:
    Code:
    int array[100];
    for (int i = 0; i < 100; i++)
    {
        array[i] = i = array[(i + 1) * 10];
    }
 
  • #3
Maybe your problem is not exactly the real problem of your program.
The biggest problem of your program exists here:
Code:
scanf("%d", &n);
Because you haven't checked the value of the input.
No one can guarantee the the input value is of type int and in the range of 0~2.
That means if the input value is not 0~2 or not even a int value, there may be some
severe errors occurrence.
 
  • #4
1) Inquisitus

Id prefer this one cause my actual array size is going to be about 100 and the values are not predictable although it is increasing.

int array[] = { 13, 21, 35, 41, 51, 63, 74, 81, 99......1000 };

2) shwx

The program that i want to write is for an ignition system where depending on the speed of the engine, i will assign a suitable time of spark. So ill assume my input is going to be either on or off and depending on the "on time", i can calculate the speed,it will then points to an address which is the time of spark. Its really an ambitious thing to do for a beginner but i decided to do it anyways.

***new question***

Eventually ill need to apply my c code to program a PIC. I am using Wiz-c as my compiler because i find that its quite easy to visualize what's happening when the code is compiled and simulated. I don't know if this is too elaborate, i hope someone can help me :

1) What is the difference between typical c code (standard stuff such as prinf,scanf) and the ones used on a pic (movlw,movwf,trisa,trisb). I was learning C and PIC code separately and i didnt know that it can be combined(see link for further details). Is the ones on the pic is just an extension of c codes needed in order to make the PIC working?

2) Can anyone give me some tips on how to progress from here on? I am quite confuse on how to write a code to calculate the rpm, there are some examples on the web on a DIY CDI but i just don't understand the logic..

code.http://www.sportdevices.com/ignition/ignition.c"
 
Last edited by a moderator:

1. What are pointers and how do they work with arrays?

Pointers are variables that store the memory address of another variable. They can be used to indirectly access or modify the value of the variable they are pointing to. Pointers and arrays are closely related because arrays are essentially a series of consecutive memory locations, and a pointer can point to the first element of an array. This allows for efficient traversal and manipulation of array elements.

2. How do you declare and initialize a pointer?

To declare a pointer variable, you use an asterisk (*) before the variable name. For example: int *ptr; To initialize a pointer, you can either assign it the address of another variable, or use the address-of operator (&) on an existing variable. For example: int num = 10; int *ptr = # This sets the pointer to point to the memory address of the variable "num".

3. Can you have an array of pointers?

Yes, you can have an array of pointers. This means that each element in the array is a pointer to a specific memory address. This can be useful for creating arrays of strings, as each element in the array can point to a different string.

4. How do you access elements in an array using pointers?

To access elements in an array using pointers, you can use pointer arithmetic. This involves incrementing or decrementing the pointer by the appropriate number of bytes to move to the next or previous element in the array. For example, to access the third element in an integer array using a pointer, you could use: *(ptr + 2) This moves the pointer two elements forward and dereferences it, giving you access to the third element.

5. What are some common errors when using pointers and arrays?

Some common errors when using pointers and arrays include dereferencing a null pointer, accessing elements outside the bounds of the array, and not properly allocating memory for a pointer before using it. These errors can lead to unexpected behavior or even crashes in a program, so it is important to carefully manage and validate pointers and arrays to avoid them.

Similar threads

  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
2
Replies
47
Views
4K
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
6
Views
5K
  • Programming and Computer Science
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top