Welcome Guest ( Log In | Register )

Forum Announcement

Please keep your account's email current. http://lowy.at/chgEmail

Outline · [ Standard ] · Linear+

> need help with c programming

views
     
TSGrammar Police
post Nov 17 2021, 11:34 PM, updated 6d ago

Casual
***
Junior Member
323 posts

Joined: Mar 2016
this c programming has been giving me lot of grief...


why is

char x[10];

scanf("%s", x);
printf("%s", x);


this works?

BUT


int n ;
scanf("%i", n);
printf("%i", n);

this doesnt work, unless you change

scanf("%i", &n);



Is it because char is an array? But shouldnt pointers only be used when there is an array? actually when should you use a pointer?

Also if you want to use string in c, you have to do like char *string, so all strings are secretly arrays?

Also why is

char *s = "xyz";

printf("%s", s);

this is OK

but

printf("%s", s[1]);

This is NOT ok

and instead need to put the & like this to work

printf("%s", &s[1]);

??
WongGei
post Nov 17 2021, 11:53 PM

Regular
******
Senior Member
1,148 posts

Joined: Dec 2007
From: Kuala Lumpur
In C, array is a kind of pointer, in short.

http://www.cs.ecu.edu/karl/3300/spr16/Note...ay/pointer.html
xboxrockers
post Nov 23 2021, 01:40 AM

On my way
****
Senior Member
642 posts

Joined: Dec 2011



scanf("%i", &n);

This is dangerous and wrong, imagine you key in 55555
but n is only 4bytes.
silverhawk
post Nov 23 2021, 10:26 AM

I'm Positively Lustrous
Group Icon
Elite
4,277 posts

Joined: Jan 2003


QUOTE(Grammar Police @ Nov 17 2021, 11:34 PM)
this doesnt work, unless you change

scanf("%i", &n);
n = value
&n = address

Think of it as "scan feed into value n" vs "scan feed into address n". Which do you think is correct?

QUOTE
Is it because char is an array? But shouldnt pointers only be used when there is an array? actually when should you use a pointer?

You will typically use pointers in function parameters to avoid passing the size of the value into the function, using unnecessary memory.

QUOTE
Also if you want to use string in c, you have to do like char *string, so all strings are secretly arrays?

Yes, its the same in almost any language. Strings are really just an array of chars, or maybe even a linked list.

QUOTE
Also why is

char *s = "xyz";
printf("%s", s);

this is OK, but

printf("%s", s[1]);

This is NOT ok

and instead need to put the & like this to work

printf("%s", &s[1]);

??
*
%s is a string, which as you've learnt, is an array of characters, so its expecting a pointer/address. s[1] gives the value, not the address. If you changed %s to %c in printf, s[1] will work.
TSGrammar Police
post Nov 23 2021, 11:54 AM

Casual
***
Junior Member
323 posts

Joined: Mar 2016
QUOTE(silverhawk @ Nov 23 2021, 10:26 AM)
n = value
&n = address

Think of it as "scan feed into value n" vs "scan feed into address n". Which do you think is correct?

*
cannot scan feed into integer and must feed into address? why is it?
silverhawk
post Nov 23 2021, 01:32 PM

I'm Positively Lustrous
Group Icon
Elite
4,277 posts

Joined: Jan 2003


QUOTE(Grammar Police @ Nov 23 2021, 11:54 AM)
cannot scan feed into integer and must feed into address? why is it?
*
because the address is where the value is stored. How do you feed into a value?

CODE

int n = 3;

//n = 3;
// &n - 0x0001

// Lets say you input 5
scanf("%d", n);


so if you feed value 5 into 3, does that make sense? Or does it make more sense to say, scan feed (with value 5) into memory address 0x0001.


flashang P
post Nov 23 2021, 11:14 PM

New Member
*
Probation
42 posts

Joined: Aug 2021


CODE

#include <stdio.h>

char x[10];

void case1() {
printf( "example 1 : \n" );
printf( "enter string x : " );
scanf( "%s", x );
printf( "string %s \n", x );
printf( "address %i \n", x );

printf( "enter string &x : " );
scanf( "%s", &x );
printf( "string %s \n", &x );
printf( "address %i \n", &x );
}

/*
example 1 :
enter string x : str1
string str1
address 4215816
enter string &x : str2
string str2
address 4215816
----------
*/


scanf() // read data and store into location. location => pointer.

* note 1 : string is array of characters.
* note 2 : scanf accept pointer, using variable name "x" or "&x" will send in pointer.

smile.gif



flashang P
post Nov 23 2021, 11:46 PM

New Member
*
Probation
42 posts

Joined: Aug 2021


CODE

int n[10];

void case2a() {
printf( "example 2a : \n" );
printf( "value of n %i \n", n );
printf( "value of n[0] n[1] %d %d \n", n[0], n[1] );
printf( "enter number n : " );
scanf( "%i", n );
printf( "value of n[0] [1] %d %d \n", n[0], n[1] );
printf( "---------- \n" );

printf( "value of &n %i \n", &n );
printf( "value of &[0] &n[1] %d %d \n", &n[0], &n[1] );
printf( "enter number &n : " );
scanf( "%i", &n );
printf( "value of n[0] n[1] %d %d \n", n[0], n[1] );
printf( "========== \n" );
}

/*
example 2a :
value of n 4215808
value of n[0] n[1] 0 0
enter number n : 456456
value of n[0] [1] 456456 0
----------
value of &n 4215808
value of &[0] &n[1] 4215808 4215812
enter number &n : 789789
value of n[0] n[1] 789789 0
==========
*/



* note 3 : n is array of int (32 bits is from -2147483648 to 2147483647)
* note 4 : using "n" or "&n" will use pointer.
* note 5 : address of n[0] and n[1] different are 4, int32 use 4 bytes.

smile.gif


flashang P
post Nov 23 2021, 11:51 PM

New Member
*
Probation
42 posts

Joined: Aug 2021


CODE

int num;

void case2b() {
printf( "example 2b : \n" );
printf( "value of num %i \n", num );
printf( "enter number n (skip) \n" );
//scanf( "%i", num );
printf( "value of num %d \n", num );

printf( "value of &num %i \n", &num );
printf( "enter number n : " );
scanf( "%i", &num );
printf( "value of num %d \n", num );
printf( "---------- \n" );
}

/*
example 2b :
value of num 0
enter number n (skip)
value of num 0
value of &num 4215776
enter number n : 123123
value of num 123123
----------
*/


* note 6 : int num is a 32bit variable, default value assume is 0.
* note 7 : if scanf store value to "address 0" or "any other unknown address" will cause system crash.
* note 8 : for non-array variable, use &num to tell scanf store value to "this location"

smile.gif


cikelempadey
post Nov 24 2021, 05:31 PM

Getting Started
**
Junior Member
81 posts

Joined: Apr 2011
From: Your Nen Nen


basically u can think of any variable in C as a storage location in computer memory.

when u declare a variable, u are allocating a mempry storage for the variable, and the variable name refers to the value resides in that memory location.

when u use scanf, it needs a storage to store the input value, ie the address of a memory location, not the value residing in that location, hence the use of & as referring the address of the memory location/variable. with array, the variable name itself refers to the address of the start of the array (e.g string of chars) so u can use the array name itself as arg to scanf without the need to use &
Horhorlidat
post Yesterday, 01:28 AM

New Member
*
Junior Member
11 posts

Joined: Jul 2015
in C, while types like int, double hold values, arrays are pointers (addresses in memory)

e.g.
int n = 5 (variable n holds integer 5)
printf(n) // prints value (5)
printf(&n) // prints 0x8f49be30 (some address in memory)

char x[10] = some letters
printf(x[n]) // prints letter (value) at specified index
printf(&x[n]) // prints pointer to specified index
printf(x) // prints pointer to the first index

As you know, arrays are indexed therefore you only get the value when you call its index. However, when calling x itself, it will return you the pointer of the address of the first index. So theres no such thing as &x (address of pointer)
x[n] returns value
x returns pointer
address : &x[0] is the same as x
address : &x[1] is the same as x+1
value : x[0] is the same as *x (dereference operator to get value pointed by pointer)
value : x[1] is the same as *(x+1)
I believe you understand how scanf works based on other comments here

This post has been edited by Horhorlidat: Yesterday, 01:47 AM

 

Change to:
| Lo-Fi Version
0.0211sec    0.91    5 queries    GZIP Disabled
Time is now: 29th November 2021 - 10:14 AM