C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-21-2009, 03:16 PM   #1
Registered User
 
Join Date: May 2009
Posts: 5
printing elements of an array using pointers

Hi,

I'm just learning about pointers and as a simple exercise I am trying to print the elements of the array. I'm trying to write a simple function that cycles through the columns and then prints each value to the screen. However, I'm having trouble calling the function properly.

Code:
/*Function the prints a single row array's contents to the screen. */

void  matrix_print(int row, int col, double *m){
  int i;
  for(i=0; i<col; i++){
   
    printf("%d  ",*(m+i*col));
  }
  printf("\n");
}

main(){

  double arr[5] = { 2.5, 3.2, 18.7, -1.35, 4.2 };
  matrix_print(1, 5, *arr[5]);  //causing an error
}
The problem in the 3rd argument to the of the matrix_print. The error says that it is an compatible type. Any suggestions?

Thanks in advance!
zeebo17 is offline   Reply With Quote
Old 05-21-2009, 03:43 PM   #2
Banned
 
ಠ_ಠ's Avatar
 
Join Date: Mar 2009
Posts: 533
your function expects a pointer to a double
you are passing in a double (located outside the bounds of arr)
__________________
╔╗╔══╦╗
║║║╔╗║║
║╚╣╚╝║╚╗
╚═╩══╩═╝
ಠ_ಠ is offline   Reply With Quote
Old 05-22-2009, 08:55 PM   #3
Registered User
 
Join Date: Apr 2009
Location: Russia
Posts: 116
Code:
    matrix_print(1, 5, arr);
and
Code:
    for (i = 0; i < col; i++)   
        printf("%g  ",*(m+row*i));
    putchar('\n');
this will print col elements from row (from m)
c.user is offline   Reply With Quote
Old 05-22-2009, 09:30 PM   #4
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
You're trying to make pointers more complex than they really are. C scales pointer arithmetic, so you don't need to calculate indices. There are two obvious ways to print out your array:
Code:
#include <stdio.h>

static void f(double *a, int n)
{
  int i;

  for(i = 0; i < n; i++)
  {
    printf("%f ", a[i]);
    /* or slightly less obvious, imo */
    printf("%f ", *(a + i));
  }
  putchar('\n');
}

int main(void)
{
  double a[5] = { 2.5, 3.2, 18.7, -1.35, 4.2 };

  f(a, 5);

  return 0;
}
You can use the array subscript operator (that is, []) on pointers. Arrays and pointers have a close relationship in C, which means that often you can treat them the same way. In fact, the following function declaration:
Code:
void f(double a[]);
is the same as:
Code:
void f(double *a);
Don't get the idea that pointers and arrays are the same thing--they're not--but their relationship is such that you can use the array operator on pointers (and, to tell the truth, you can only use it on pointers, but that's another discussion).
cas is offline   Reply With Quote
Reply

Tags
arrays, pointers

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic array of structures containing yet another dynamic array of structures innqubus C Programming 2 07-11-2008 07:39 AM
using realloc for a dynamically growing array broli86 C Programming 10 06-27-2008 05:37 AM
Syntax for constant array of array pointers BMintern C Programming 4 05-14-2008 08:21 AM
Is there a way to find the number of elements in an array? lime C Programming 2 08-03-2003 10:01 AM
how to create a linked list of items using pointers stored in a two dimensional array Unregistered C Programming 5 11-20-2001 12:48 PM


All times are GMT -6. The time now is 11:05 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22