PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, July 5, 2022

[FIXED] How to pass the address of array[2] as a reference to a function in python

 July 05, 2022     arrays, c, pass-by-reference, python     No comments   

Issue

im trying to pass the address of an array reg_value[2] to a function in python

import array
reg_value = array.array('I',[0]*100)

def postdata(data):
        for i in range(2):
            print(data[i])

reg_value[0]=1
reg_value[1]=2
reg_value[2]=3
reg_value[3]=4

postdata(reg_value)

the above code prints the value 1 2

but i need to pass the address of reg_value[2] to print the value 3 4

i can achieve this in C programming like passing the address as &reg_value[2]

how to replicate the same in python?


Solution

You can't pass the address of anything in Python, but you can pass a slice which contains a subsequence.

postdata(reg_value[2:])

This does create a copy of the items from the sequence. Probably a better design is to allow the caller to specify a base index.

def postdata(items, offset=0):
    for i in range(2):
        print(items[offset+i])


Answered By - tripleee
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing