Z
Zach
I am working on a new version of my swap program [1] that uses
pointers. It seems to work fine but I have some points I would like
clarifying.
Here is the code:
/* swap-foo2.c: swap 2 integers
* Zach
* 01/27/08, version 2
*/
#include <stdio.h>
#include <stdlib.h>
int swap(int*, int*);
int *s1, *s2;
int main(void)
{
s1 = 3;
s2 = 4;
printf("s1 = %d \n", s1);
printf("s2 = %d \n", s2);
swap(s1,s2);
exit(EXIT_SUCCESS);
}
int swap(int *s1, int *s2)
{
int temp;
temp = s1;
s1 = s2;
s2 = temp;
printf("s1 = %d \n", s1);
printf("s2 = %d \n", s2);
return s1,s2;
}
When I compile and run it:
zu22@netrek:~/src/myc$ gcc -g -o foo swap-foo2.c
swap-foo2.c: In function 'main':
swap-foo2.c:16: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:17: warning: assignment makes pointer from integer without
a cast
swap-foo2.c: In function 'swap':
swap-foo2.c:32: warning: assignment makes integer from pointer without
a cast
swap-foo2.c:34: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:39: warning: return makes integer from pointer without a
cast
zu22@netrek:~/src/myc$ ./foo
s1 = 3
s2 = 4
s1 = 4
s2 = 3
I had been told by a member of this group, Joachim Schmitz, to call my
swap function with the address of my two variables:
swap(&s1,&s2);
However when I do this it gives some new warnings and a strange
result:
zu22@netrek:~/src/myc$ gcc -g -o foo swap-foo2.c
swap-foo2.c: In function 'main':
swap-foo2.c:16: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:17: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:22: warning: passing argument 1 of 'swap' from
incompatible pointer type
swap-foo2.c:22: warning: passing argument 2 of 'swap' from
incompatible pointer type
swap-foo2.c: In function 'swap':
swap-foo2.c:32: warning: assignment makes integer from pointer without
a cast
swap-foo2.c:34: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:39: warning: return makes integer from pointer without a
cast
zu22@netrek:~/src/myc$ ./foo
s1 = 3
s2 = 4
s1 = 134518352
s2 = 134518348
Why is this? I'm doing this to help advance my understanding of
pointers and passing parameters to functions. Can someone explain what
all these warnings mean?
BTW on lines 16, 17 where I assign values to the pointers why can't I
do this:
*s1 = 3;
*s2 = 4;
When I do it causes a core dump:
zu22@netrek:~/src/myc$ gcc -g -o foo swap-foo2.c
swap-foo2.c: In function 'main':
swap-foo2.c:22: warning: passing argument 1 of 'swap' from
incompatible pointer type
swap-foo2.c:22: warning: passing argument 2 of 'swap' from
incompatible pointer type
swap-foo2.c: In function 'swap':
swap-foo2.c:32: warning: assignment makes integer from pointer without
a cast
swap-foo2.c:34: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:39: warning: return makes integer from pointer without a
cast
zu22@netrek:~/src/myc$ gdb ./foo
GNU gdb 6.6.90.20070912-debian
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/
gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show
copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
Using host libthread_db library "/lib/libthread_db.so.1".
(gdb) run
Starting program: /home/zu22/src/myc/foo
Failed to read a valid object file image from memory.
Program received signal SIGSEGV, Segmentation fault.
0x080483ba in main () at swap-foo2.c:16
16 *s1 = 3;
(gdb) bt
#0 0x080483ba in main () at swap-foo2.c:16
(gdb) quit
The program is running. Exit anyway? (y or n) y
I've seen pointer values assigned in this way with type char as in:
char *string = "hello";
So why is this illegal with type int?
Also I noticed if I print out the values of my pointer variables in
main after the call to swap they are still the same. My understanding
was that since I declared them as global variables they had global
scope and when swap returns the new values they should be changed but
it seems they aren't here:
/* swap-foo2.c: swap 2 integers
* Zach
* 01/27/08, version 2
*/
#include <stdio.h>
#include <stdlib.h>
int swap(int*, int*);
int *s1, *s2;
int main(void)
{
s1 = 3;
s2 = 4;
printf("s1 = %d \n", s1);
printf("s2 = %d \n", s2);
swap(s1,s2);
printf("s1 = %d \n", s1);
printf("s2 = %d \n", s2);
exit(EXIT_SUCCESS);
}
int swap(int *s1, int *s2)
{
int temp;
temp = s1;
s1 = s2;
s2 = temp;
return s1,s2;
}
zu22@netrek:~/src/myc$ gcc -g -o foo swap-foo2.c
swap-foo2.c: In function 'main':
swap-foo2.c:16: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:17: warning: assignment makes pointer from integer without
a cast
swap-foo2.c: In function 'swap':
swap-foo2.c:35: warning: assignment makes integer from pointer without
a cast
swap-foo2.c:37: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:39: warning: return makes integer from pointer without a
cast
zu22@netrek:~/src/myc$ ./foo
s1 = 3
s2 = 4
s1 = 3
s2 = 4
My understanding is that in the following code:
int main(void)
{
int *p; /* or "int* p" declares a variable p pointing to type int */
int q; /* declares variable q of type int */
p = 3; /* assigns the integer 3 as the pointer-value */
foo(&p): /* the function foo calls the address of the pointer p's
location in memory,
by using the address-of operator &.
this represents the starting bit of where it is layed
out in memory */
q = *p; /* assigns the pointer-value to point to variable q,
by using the dereferencing operator *. */
}
Are all my comments correct?
PS: I noticed in "man 3 printf" that %p is for pointers and not %d but
when I use that it prints my results in hexadecimal such as:
s1 = 0x3
s2 = 0x4
[1] http://tinyurl.com/2zh5mj
Regards,
Zach
pointers. It seems to work fine but I have some points I would like
clarifying.
Here is the code:
/* swap-foo2.c: swap 2 integers
* Zach
* 01/27/08, version 2
*/
#include <stdio.h>
#include <stdlib.h>
int swap(int*, int*);
int *s1, *s2;
int main(void)
{
s1 = 3;
s2 = 4;
printf("s1 = %d \n", s1);
printf("s2 = %d \n", s2);
swap(s1,s2);
exit(EXIT_SUCCESS);
}
int swap(int *s1, int *s2)
{
int temp;
temp = s1;
s1 = s2;
s2 = temp;
printf("s1 = %d \n", s1);
printf("s2 = %d \n", s2);
return s1,s2;
}
When I compile and run it:
zu22@netrek:~/src/myc$ gcc -g -o foo swap-foo2.c
swap-foo2.c: In function 'main':
swap-foo2.c:16: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:17: warning: assignment makes pointer from integer without
a cast
swap-foo2.c: In function 'swap':
swap-foo2.c:32: warning: assignment makes integer from pointer without
a cast
swap-foo2.c:34: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:39: warning: return makes integer from pointer without a
cast
zu22@netrek:~/src/myc$ ./foo
s1 = 3
s2 = 4
s1 = 4
s2 = 3
I had been told by a member of this group, Joachim Schmitz, to call my
swap function with the address of my two variables:
swap(&s1,&s2);
However when I do this it gives some new warnings and a strange
result:
zu22@netrek:~/src/myc$ gcc -g -o foo swap-foo2.c
swap-foo2.c: In function 'main':
swap-foo2.c:16: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:17: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:22: warning: passing argument 1 of 'swap' from
incompatible pointer type
swap-foo2.c:22: warning: passing argument 2 of 'swap' from
incompatible pointer type
swap-foo2.c: In function 'swap':
swap-foo2.c:32: warning: assignment makes integer from pointer without
a cast
swap-foo2.c:34: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:39: warning: return makes integer from pointer without a
cast
zu22@netrek:~/src/myc$ ./foo
s1 = 3
s2 = 4
s1 = 134518352
s2 = 134518348
Why is this? I'm doing this to help advance my understanding of
pointers and passing parameters to functions. Can someone explain what
all these warnings mean?
BTW on lines 16, 17 where I assign values to the pointers why can't I
do this:
*s1 = 3;
*s2 = 4;
When I do it causes a core dump:
zu22@netrek:~/src/myc$ gcc -g -o foo swap-foo2.c
swap-foo2.c: In function 'main':
swap-foo2.c:22: warning: passing argument 1 of 'swap' from
incompatible pointer type
swap-foo2.c:22: warning: passing argument 2 of 'swap' from
incompatible pointer type
swap-foo2.c: In function 'swap':
swap-foo2.c:32: warning: assignment makes integer from pointer without
a cast
swap-foo2.c:34: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:39: warning: return makes integer from pointer without a
cast
zu22@netrek:~/src/myc$ gdb ./foo
GNU gdb 6.6.90.20070912-debian
Copyright (C) 2007 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/
gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show
copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
Using host libthread_db library "/lib/libthread_db.so.1".
(gdb) run
Starting program: /home/zu22/src/myc/foo
Failed to read a valid object file image from memory.
Program received signal SIGSEGV, Segmentation fault.
0x080483ba in main () at swap-foo2.c:16
16 *s1 = 3;
(gdb) bt
#0 0x080483ba in main () at swap-foo2.c:16
(gdb) quit
The program is running. Exit anyway? (y or n) y
I've seen pointer values assigned in this way with type char as in:
char *string = "hello";
So why is this illegal with type int?
Also I noticed if I print out the values of my pointer variables in
main after the call to swap they are still the same. My understanding
was that since I declared them as global variables they had global
scope and when swap returns the new values they should be changed but
it seems they aren't here:
/* swap-foo2.c: swap 2 integers
* Zach
* 01/27/08, version 2
*/
#include <stdio.h>
#include <stdlib.h>
int swap(int*, int*);
int *s1, *s2;
int main(void)
{
s1 = 3;
s2 = 4;
printf("s1 = %d \n", s1);
printf("s2 = %d \n", s2);
swap(s1,s2);
printf("s1 = %d \n", s1);
printf("s2 = %d \n", s2);
exit(EXIT_SUCCESS);
}
int swap(int *s1, int *s2)
{
int temp;
temp = s1;
s1 = s2;
s2 = temp;
return s1,s2;
}
zu22@netrek:~/src/myc$ gcc -g -o foo swap-foo2.c
swap-foo2.c: In function 'main':
swap-foo2.c:16: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:17: warning: assignment makes pointer from integer without
a cast
swap-foo2.c: In function 'swap':
swap-foo2.c:35: warning: assignment makes integer from pointer without
a cast
swap-foo2.c:37: warning: assignment makes pointer from integer without
a cast
swap-foo2.c:39: warning: return makes integer from pointer without a
cast
zu22@netrek:~/src/myc$ ./foo
s1 = 3
s2 = 4
s1 = 3
s2 = 4
My understanding is that in the following code:
int main(void)
{
int *p; /* or "int* p" declares a variable p pointing to type int */
int q; /* declares variable q of type int */
p = 3; /* assigns the integer 3 as the pointer-value */
foo(&p): /* the function foo calls the address of the pointer p's
location in memory,
by using the address-of operator &.
this represents the starting bit of where it is layed
out in memory */
q = *p; /* assigns the pointer-value to point to variable q,
by using the dereferencing operator *. */
}
Are all my comments correct?
PS: I noticed in "man 3 printf" that %p is for pointers and not %d but
when I use that it prints my results in hexadecimal such as:
s1 = 0x3
s2 = 0x4
[1] http://tinyurl.com/2zh5mj
Regards,
Zach