K
Kiran
Hi all,
What I am trying to do is to pass a pointer to the first element of
an array to a function, modify it in that function, and then print out
the values of the array (which has been modified by the function) in
main. However, I am getting a segmentation fault. Here is the code:
(Please note, the size is fixed in this code, but in my code where I am
actually going to use this, the size of the array is not known until
you get to the modify function which is why I am using pointer
notation).
#include <stdio.h>
void modifiy( int* nums );
int main() {
int* nums;
int i;
for (i=0; i < 5; i++) {
printf("%d", nums);
}
}
void modify( int* nums ) {
int i;
nums = (int*)malloc( 5*sizeof(int));
for (i=0; i < 5; i++) {
nums = i;
}
}
Once I can get this to work, I want to do the same thing w/ double
arrays, so if there are any pitfalls associated with that, any help
would be greatly appreciated.
thanks!
What I am trying to do is to pass a pointer to the first element of
an array to a function, modify it in that function, and then print out
the values of the array (which has been modified by the function) in
main. However, I am getting a segmentation fault. Here is the code:
(Please note, the size is fixed in this code, but in my code where I am
actually going to use this, the size of the array is not known until
you get to the modify function which is why I am using pointer
notation).
#include <stdio.h>
void modifiy( int* nums );
int main() {
int* nums;
int i;
for (i=0; i < 5; i++) {
printf("%d", nums);
}
}
void modify( int* nums ) {
int i;
nums = (int*)malloc( 5*sizeof(int));
for (i=0; i < 5; i++) {
nums = i;
}
}
Once I can get this to work, I want to do the same thing w/ double
arrays, so if there are any pitfalls associated with that, any help
would be greatly appreciated.
thanks!